
# File-Name:	survey-recoding-functions-US.R
# Date:	February 1, 2021
# Author: Andreu Casas / Bernhard Clemm 
# Purpose: Contains functions to be used at beginning of analysis of US surveys

# Variables are recoded/renamed according to codebook:
## https://docs.google.com/spreadsheets/d/1XGi3FluO304Sjd8AS9JjF2tHpjrplBd9EDlMziyPu9s/edit#gid=1932063479

# Example of workflow:
## Load the waves you want to recode/join
### path <- "/Users/bernhardclemm/Dropbox/Academia/EXPO/US survey rawing/drive-download-20210604T104114Z-001/"
### data0a <- read.csv(paste0(path, "US_survey_w0_lucid_raw.csv"))
### data0b <- read.csv(paste0(path, "US_survey_w0_qualtrics_raw.csv"))
### data1 <- read.csv(paste0(path, "US_survey_w1_raw.csv"))
### data2 <- read.csv(paste0(path, "US_survey_w2_raw.csv"))
### data3 <- read.csv(paste0(path, "US_survey_w3_raw.csv"))
## Either first use the recode_US_w[number] functions and then join_US_waves function
## Or use the recode_join_US function which does everything in one go

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# PACKAGES
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

library(tidyverse)

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# W0 ####
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

recode_US_w0 <- function(data) {
  
  data <- data %>%
    rename(
      "start_date_us_w0" = StartDate_w0,
      "end_date_us_w0" = EndDate,
      "meta_status_w0" = Status,
      "meta_progress_w0" = Progress,
      "meta_duration_w0" = Duration..in.seconds.,
      "meta_finished_w0" = Finished,
      "meta_recdate_w0" = RecordedDate,
      "meta_distribution_w0" = DistributionChannel,
      "meta_language_w0" = UserLanguage, 
      "meta_browser_w0" = Q30_Browser,
      "meta_version_w0" = Q30_Version,
      "meta_os_w0" = Q30_Operating.System,
      "meta_res_w0" = Q30_Resolution,
      "age_w0" = AGE_1,
      "gender_w0" = GENDER,
      "zip_w0" = ZIP,
      "ethn_w0" = ETHN,
      "ethn_text_w0" = ETHN_7_TEXT,
      "hisp_w0" = HISP,
      "edu_w0" = EDU,
      "party_w0" = PARTY,
      "ideo_w0" = IDEO_1,
      "follow_w0" = FOLLOW,
      "ethn_w0" = ETHN,
      "retake_w0" = retake,
      "ideo_score_w0" = ideo_score,
      "affiliation_w0" = affiliation,
      "affil_party_w0" = affil_party,
      "opposite_w0" = opposite,
      "op_party_w0" = op_party,
      "opposite_2_w0" = opposite_2,
      "op_party_2_w0" = op_party_2,
      "ideology_w0" = ideology
    ) %>%
    mutate(
      age_w0 = age_w0 + 16,
      gender_w0 = gender_w0 - 1,
       gender_w0_fac = as.factor(case_when(
         gender_w0 == 0 ~ "Male",
         gender_w0 == 1 ~ "Female")),
       ethn_w0_fac = as.factor(case_when(
         ethn_w0 == 1 ~ "White",
         ethn_w0 == 2 ~ "Black",
         ethn_w0 == 3 ~ "Asian",
         ethn_w0 == 4 ~ "Native American",
         ethn_w0 == 7 ~ "Prefer to self-describe")),
       hisp_w0_fac = as.factor(case_when(
         hisp_w0 == 1 ~ "yes",
         hisp_w0 == 2 ~ "no")),
       edu_w0_fac = as.factor(case_when(
         edu_w0 == 1 ~ "3rd Grade or less",
         edu_w0 == 5 ~ "Middle School - Grades 4-8",
         edu_w0 == 6 ~ "Completed some high school",
         edu_w0 == 8 ~ "High school graduate",
         edu_w0 == 9 ~ "Other post-high school vocational training",
         edu_w0 == 10 ~ "Completed some college, but no degree",
         edu_w0 == 11 ~ "Associate Degree",
         edu_w0 == 12 ~ "College Degree (such as B.A., B.S.)",
         edu_w0 == 13 ~ "Completed some graduate, but no degree",
         edu_w0 == 14 ~ "Master's degree",
         edu_w0 == 15 ~ "Doctorate degree")),
      party_w0_fac = as.factor(case_when(
        party_w0 == 1 ~ "A strong Democrat",
        party_w0 == 2 ~ "A not very strong Democrat",
        party_w0 == 3 ~ "Independent, lean toward Democrat",
        party_w0 == 4 ~ "Independent (close to neither parity)",
        party_w0 == 5 ~ "Independent, lean toward Republican",
        party_w0 == 6 ~ "A not very strong Republican",
        party_w0 == 7 ~ "A strong Republican")),
      party_w0_cat = case_when(
        party_w0 < 4 ~ "Democrat",
        party_w0 == 4 ~ "Independent",
        party_w0 > 4 ~ "Republican")) %>%
    
    mutate(start_date_us_w0 = as.POSIXct(
      start_date_us_w0, format = "%Y-%m-%d %H:%M:%S", tz = "MST"),
      end_date_us_w0 = as.POSIXct(
        end_date_us_w0, format = "%Y-%m-%d %H:%M:%S", tz = "MST"))
  
}

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# W1 recoding ####
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

recode_US_w1 <- function(data) {
  
  data <- data %>%

    # Take care of some Qualtrics coding errors
    mutate(
      ISPERSO_1 = case_when(ISPERSO_1 == 8 ~ 6,
                            ISPERSO_1 == 9 ~ 7,
                            TRUE ~ as.numeric(ISPERSO_1)),
      ISPERSO_2 = case_when(ISPERSO_2 == 8 ~ 6,
                            ISPERSO_2 == 9 ~ 7,
                            TRUE ~ as.numeric(ISPERSO_2)),
      ISPERSO_3 = case_when(ISPERSO_3 == 8 ~ 6,
                            ISPERSO_3 == 9 ~ 7,
                            TRUE ~ as.numeric(ISPERSO_3)),
      ISPERSO_4 = case_when(ISPERSO_4 == 8 ~ 6,
                            ISPERSO_4 == 9 ~ 7,
                            TRUE ~ as.numeric(ISPERSO_4)),
      ISPERSO_5 = case_when(ISPERSO_5 == 8 ~ 6,
                            ISPERSO_5 == 9 ~ 7,
                            TRUE ~ as.numeric(ISPERSO_5)),
      ISPERSO_6 = case_when(ISPERSO_6 == 8 ~ 6,
                            ISPERSO_6 == 9 ~ 7,
                            TRUE ~ as.numeric(ISPERSO_6)),
      ECON_1 = case_when(ECON_1 == 101 ~ 8,
                         ECON_1 == 102 ~ 9,
                         ECON_1 == 103 ~ 10,
                         ECON_1 == 104 ~ 11,
                         ECON_1 == 105 ~ 12,
                         ECON_1 == 106 ~ 13,
                         TRUE ~ as.numeric(ECON_1)),
      ECON_2 = case_when(ECON_2 == 101 ~ 8,
                         ECON_2 == 102 ~ 9,
                         ECON_2 == 103 ~ 10,
                         ECON_2 == 104 ~ 11,
                         ECON_2 == 105 ~ 12,
                         ECON_2 == 106 ~ 13,
                         TRUE ~ as.numeric(ECON_2)),
      ECON_3 = case_when(ECON_3 == 101 ~ 8,
                         ECON_3 == 102 ~ 9,
                         ECON_3 == 103 ~ 10,
                         ECON_3 == 104 ~ 11,
                         ECON_3 == 105 ~ 12,
                         ECON_3 == 106 ~ 13,
                         TRUE ~ as.numeric(ECON_3))
      ) %>%
    # Recode all "personally important issue" variables 
    # and split variables for Independents vs. non-Independents
    mutate(
      # Issue strong view 
      strong_welfare_w1 = ifelse(ISSUE == 3, ISSTRONG_6, NA),
      strong_health_w1 = ifelse(ISSUE == 4, ISSTRONG_6, NA),
      strong_civil_w1 = ifelse(ISSUE == 6, ISSTRONG_6, NA),
      strong_crime_w1 = ifelse(ISSUE == 7, ISSTRONG_6, NA),
      strong_foreign_w1 = ifelse(ISSUE == 8, ISSTRONG_6, NA),
      strong_supreme_w1 = ifelse(ISSUE == 16, ISSTRONG_6, NA),
      # Issue relevance
      important_welfare_w1 = ifelse(ISSUE == 3, ISPERSO_6, NA),
      important_health_w1 = ifelse(ISSUE == 4, ISPERSO_6, NA),
      important_civil_w1 = ifelse(ISSUE == 6, ISPERSO_6, NA),
      important_crime_w1 = ifelse(ISSUE == 7, ISPERSO_6, NA),
      important_foreign_w1 = ifelse(ISSUE == 8, ISPERSO_6, NA),
      important_supreme_w1 = ifelse(ISSUE == 16, ISPERSO_6, NA),
      # Disagreement in personal discussion
      disagr_talk_gun_w1 = ifelse(ISDISPERS_1 != 8, ISDISPERS_1, 0),
      disagr_talk_immigrant_w1 = ifelse(ISDISPERS_2 != 8, ISDISPERS_2, 0),
      disagr_talk_climate_w1 = ifelse(ISDISPERS_3 != 8, ISDISPERS_3, 0),
      disagr_talk_trump_w1 = ifelse(ISDISPERS_4 != 8, ISDISPERS_4, 0),
      disagr_talk_econ_w1 = ifelse(ISDISPERS_5 != 8, ISDISPERS_5, 0),
      disagr_talk_welfare_w1 = ifelse(ISSUE == 3, ifelse(ISDISPERS_6 != 8, ISDISPERS_6, 0), NA),
      disagr_talk_health_w1 = ifelse(ISSUE == 4, ifelse(ISDISPERS_6 != 8, ISDISPERS_6, 0), NA),
      disagr_talk_civil_w1 = ifelse(ISSUE == 6, ifelse(ISDISPERS_6 != 8, ISDISPERS_6, 0), NA),
      disagr_talk_crime_w1 = ifelse(ISSUE == 7, ifelse(ISDISPERS_6 != 8, ISDISPERS_6, 0), NA),
      disagr_talk_foreign_w1 = ifelse(ISSUE == 8, ifelse(ISDISPERS_6 != 8, ISDISPERS_6, 0), NA),
      disagr_talk_supreme_w1 = ifelse(ISSUE == 16, ifelse(ISDISPERS_6 != 8, ISDISPERS_6, 0), NA),
      # Disagreement media offline
      disagr_offline_gun_w1 = ifelse(ISDISMEDIA_1 != 8, ISDISMEDIA_1, 0),
      disagr_offline_immigrant_w1 = ifelse(ISDISMEDIA_2 != 8, ISDISMEDIA_2, 0),
      disagr_offline_climate_w1 = ifelse(ISDISMEDIA_3 !=8, ISDISMEDIA_3, 0),
      disagr_offline_trump_w1 = ifelse(ISDISMEDIA_4 != 8, ISDISMEDIA_4, 0),
      disagr_offline_econ_w1 = ifelse(ISDISMEDIA_5 != 8, ISDISMEDIA_5, 0),
      disagr_offline_welfare_w1 = ifelse(ISSUE == 3, ifelse(ISDISMEDIA_6 != 8, ISDISMEDIA_6, 0), NA),
      disagr_offline_health_w1 = ifelse(ISSUE == 4, ifelse(ISDISMEDIA_6 != 8, ISDISMEDIA_6, 0), NA),
      disagr_offline_civil_w1 = ifelse(ISSUE == 6, ifelse(ISDISMEDIA_6 != 8, ISDISMEDIA_6, 0), NA),
      disagr_offline_crime_w1 = ifelse(ISSUE == 7, ifelse(ISDISMEDIA_6 != 8, ISDISMEDIA_6, 0), NA),
      disagr_offline_foreign_w1 = ifelse(ISSUE == 8, ifelse(ISDISMEDIA_6 != 8, ISDISMEDIA_6, 0), NA),
      disagr_offline_supreme_w1 = ifelse(ISSUE == 16, ifelse(ISDISMEDIA_6 != 8, ISDISMEDIA_6, 0), NA),
      # Disagreement media online
      disagr_internet_gun_w1 = ifelse(ISDISONLINE_1 != 8, ISDISONLINE_1, 0),
      disagr_internet_immigrant_w1 = ifelse(ISDISONLINE_2 != 8, ISDISONLINE_2, 0),
      disagr_internet_trump_w1 = ifelse(ISDISONLINE_4 != 8, ISDISONLINE_4, 0),
      disagr_internet_climate_w1 = ifelse(ISDISONLINE_3 != 8, ISDISONLINE_3, 0),
      disagr_internet_econ_w1 = ifelse(ISDISONLINE_5 != 8, ISDISONLINE_5, 0),
      disagr_internet_welfare_w1 = ifelse(ISSUE == 3, ifelse(ISDISONLINE_6 != 8, ISDISONLINE_6, 0), NA),
      disagr_internet_health_w1 = ifelse(ISSUE == 4, ifelse(ISDISONLINE_6 != 8, ISDISONLINE_6, 0), NA),
      disagr_internet_civil_w1 = ifelse(ISSUE == 6, ifelse(ISDISONLINE_6 != 8, ISDISONLINE_6, 0), NA),
      disagr_internet_crime_w1 = ifelse(ISSUE == 7, ifelse(ISDISONLINE_6 != 8, ISDISONLINE_6, 0), NA),
      disagr_internet_foreign_w1 = ifelse(ISSUE == 8, ifelse(ISDISONLINE_6 != 8, ISDISONLINE_6, 0), NA),
      disagr_internet_supreme_w1 = ifelse(ISSUE == 16, ifelse(ISDISONLINE_6 != 8,ISDISONLINE_6, 0), NA),
      # Disagreement social media
      disagr_sm_gun_w1 = ifelse(ISDISSOCIAL_1 != 8, ISDISSOCIAL_1, 0),
      disagr_sm_immigrant_w1 = ifelse(ISDISSOCIAL_2 != 8, ISDISSOCIAL_2, 0),
      disagr_sm_trump_w1 = ifelse(ISDISSOCIAL_4 != 8, ISDISSOCIAL_4, 0),
      disagr_sm_climate_w1 = ifelse(ISDISSOCIAL_3 != 8, ISDISSOCIAL_3, 0),
      disagr_sm_econ_w1 = ifelse(ISDISSOCIAL_5 != 8, ISDISSOCIAL_5, 0),
      disagr_sm_welfare_w1 = ifelse(ISSUE == 3, ifelse(ISDISSOCIAL_6 != 8, ISDISSOCIAL_6, 0), NA),
      disagr_sm_health_w1 = ifelse(ISSUE == 4, ifelse(ISDISSOCIAL_6 != 8, ISDISSOCIAL_6, 0), NA),
      disagr_sm_civil_w1 = ifelse(ISSUE == 6, ifelse(ISDISSOCIAL_6 != 8, ISDISSOCIAL_6, 0), NA),
      disagr_sm_crime_w1 = ifelse(ISSUE == 7, ifelse(ISDISSOCIAL_6 != 8, ISDISSOCIAL_6, 0), NA),
      disagr_sm_foreign_w1 = ifelse(ISSUE == 8, ifelse(ISDISSOCIAL_6 != 8, ISDISSOCIAL_6, 0), NA),
      disagr_sm_supreme_w1 = ifelse(ISSUE == 16, ifelse(ISDISSOCIAL_6 != 8, ISDISSOCIAL_6, 0), NA),
      # Disagreement social media
      disagr_msg_gun_w1 = ifelse(ISDISMESSENGE_1 != 8, ISDISMESSENGE_1, 0),
      disagr_msg_immigrant_w1 = ifelse(ISDISMESSENGE_2 != 8, ISDISMESSENGE_2, 0),
      disagr_msg_trump_w1 = ifelse(ISDISMESSENGE_4 != 8, ISDISMESSENGE_4, 0),
      disagr_msg_climate_w1 = ifelse(ISDISMESSENGE_3 != 8, ISDISMESSENGE_3, 0),
      disagr_msg_econ_w1 = ifelse(ISDISMESSENGE_5 != 8, ISDISMESSENGE_5, 0),
      disagr_msg_welfare_w1 = ifelse(ISSUE == 3, ifelse(ISDISMESSENGE_6 != 8, ISDISMESSENGE_6, 0), NA),
      disagr_msg_health_w1 = ifelse(ISSUE == 4, ifelse(ISDISMESSENGE_6 != 8, ISDISMESSENGE_6, 0), NA),
      disagr_msg_civil_w1 = ifelse(ISSUE == 6, ifelse(ISDISMESSENGE_6 != 8, ISDISMESSENGE_6, 0), NA),
      disagr_msg_crime_w1 = ifelse(ISSUE == 7, ifelse(ISDISMESSENGE_6 != 8, ISDISMESSENGE_6, 0), NA),
      disagr_msg_foreign_w1 = ifelse(ISSUE == 8, ifelse(ISDISMESSENGE_6 != 8, ISDISMESSENGE_6, 0), NA),
      disagr_msg_supreme_w1 = ifelse(ISSUE == 16, ifelse(ISDISMESSENGE_6 != 8, ISDISMESSENGE_6, 0), NA),
      # Respect for people with opposite opinion
      respect_welfare_w1 = ifelse(ISSUE == 3, GRRESPECT_6, NA),
      respect_health_w1 = ifelse(ISSUE == 4, GRRESPECT_6, NA),
      respect_civil_w1 = ifelse(ISSUE == 6, GRRESPECT_6, NA),
      respect_crime_w1 = ifelse(ISSUE == 7, GRRESPECT_6, NA),
      respect_foreign_w1 = ifelse(ISSUE == 8, GRRESPECT_6, NA),
      respect_supreme_w1 = ifelse(ISSUE == 16, GRRESPECT_6, NA),
      respect_party_opp_w1 = ifelse(affiliation == "Democrat" | affiliation == "Republican", GRRESPECT_7, NA),
      respect_ind_dem_w1 = ifelse(affiliation == "Independent", GRRESPECT_7, NA),
      respect_ind_rep_w1 = ifelse(affiliation == "Independent", GRRESPECT_8, NA),
      respect_ideo_opp_w1 = ifelse(ideology == "left" | ideology == "right", GRRESPECT_9, NA),
      respect_ind_cons = ifelse(ideology == "middle", GRRESPECT_9, NA),
      respect_ind_lib = ifelse(ideology == "middle", GRRESPECT_10, NA),
      # Understanding for people with opposite opinion
      understand_welfare_w1 = ifelse(ISSUE == 3, GRUNDERSTAND_6, NA),
      understand_health_w1 = ifelse(ISSUE == 4, GRUNDERSTAND_6, NA),
      understand_civil_w1 = ifelse(ISSUE == 6, GRUNDERSTAND_6, NA),
      understand_crime_w1 = ifelse(ISSUE == 7, GRUNDERSTAND_6, NA),
      understand_foreign_w1 = ifelse(ISSUE == 8, GRUNDERSTAND_6, NA),
      understand_supreme_w1 = ifelse(ISSUE == 16, GRUNDERSTAND_6, NA),
      understand_party_opp_w1 = ifelse(affiliation == "Democrat" | affiliation == "Republican", GRUNDERSTAND_7, NA),
      understand_ind_dem_w1 = ifelse(affiliation == "Independent", GRUNDERSTAND_7, NA),
      understand_ind_rep_w1 = ifelse(affiliation == "Independent", GRUNDERSTAND_8, NA),
      understand_ideo_opp_w1 = ifelse(ideology == "left" | ideology == "right", GRUNDERSTAND_9, NA),
      understand_ind_cons_w1 = ifelse(ideology == "middle", GRUNDERSTAND_9, NA),
      understand_ind_lib_w1 = ifelse(ideology == "middle", GRUNDERSTAND_10, NA),
      # FT for people with opposite opinion
      ft_welfare_w1 = ifelse(ISSUE == 3, THERMO_13, NA),
      ft_health_w1 = ifelse(ISSUE == 4, THERMO_13, NA),
      ft_civil_w1 = ifelse(ISSUE == 6, THERMO_13, NA),
      ft_crime_w1 = ifelse(ISSUE == 7, THERMO_13, NA),
      ft_foreign_w1 = ifelse(ISSUE == 8, THERMO_13, NA),
      ft_supreme_w1 = ifelse(ISSUE == 16, THERMO_13, NA),
      # Evaluating others
      mean_party_opp_w1 = ifelse(affiliation == "Democrat" | affiliation == "Republican", ATTOTHER2_1, NA), 
      mean_ind_dem_w1 = ifelse(affiliation == "Independent", ATTOTHER2_1, NA), 
      selfish_party_opp_w1 = ifelse(affiliation == "Democrat" | affiliation == "Republican", ATTOTHER2_2, NA), 
      selfish_ind_dem_w1 = ifelse(affiliation == "Independent", ATTOTHER2_2, NA), 
      stupid_party_opp_w1 = ifelse(affiliation == "Democrat" | affiliation == "Republican", ATTOTHER2_3, NA), 
      stupid_ind_dem_w1 = ifelse(affiliation == "Independent", ATTOTHER2_3, NA), 
      talk_party_opp_w1 = ifelse(affiliation == "Democrat" | affiliation == "Republican", ATTOTHER3_1, NA), 
      talk_ind_dem_w1 = ifelse(affiliation == "Independent", ATTOTHER3_1, NA), 
      findout_party_opp_w1 = ifelse(affiliation == "Democrat" | affiliation == "Republican", ATTOTHER3_2, NA), 
      findout_ind_dem_w1 = ifelse(affiliation == "Independent", ATTOTHER3_2, NA), 
      spendtime_party_opp_w1 = ifelse(affiliation == "Democrat" | affiliation == "Republican", ATTOTHER3_3, NA), 
      spendtime_ind_dem_w1 = ifelse(affiliation == "Independent", ATTOTHER3_3, NA), 
      mean_ind_rep_w1 = ifelse(affiliation == "Independent", ATTOTHER_2_IND_1, NA), 
      selfish_ind_rep_w1 = ifelse(affiliation == "Independent", ATTOTHER_2_IND_2, NA), 
      stupid_ind_rep_w1 = ifelse(affiliation == "Independent", ATTOTHER_2_IND_3, NA), 
      talk_ind_rep_w1 = ifelse(affiliation == "Independent", ATTOTHER_3_IND_1, NA), 
      findout_ind_rep_w1 = ifelse(affiliation == "Independent", ATTOTHER_3_IND_2, NA), 
      spendtime_ind_rep_w1 = ifelse(affiliation == "Independent", ATTOTHER_3_IND_3, NA), 
      
      mean_ideo_opp_w1 = ifelse(ideology == "left" | ideology == "right", ATTIDEO1_1, NA), 
      mean_ind_cons_w1 = ifelse(ideology == "middle", ATTIDEO1_1, NA), 
      selfish_ideo_opp_w1 = ifelse(ideology == "left" | ideology == "right", ATTIDEO1_2, NA), 
      selfish_ind_cons_w1 = ifelse(ideology == "middle", ATTIDEO1_2, NA), 
      stupid_ideo_opp_w1 = ifelse(ideology == "left" | ideology == "right", ATTIDEO1_3, NA), 
      stupid_ind_cons_w1 = ifelse(ideology == "middle", ATTIDEO1_3, NA), 
      talk_ideo_opp_w1 = ifelse(ideology == "left" | ideology == "right", ATTIDEO02_1, NA), 
      talk_ind_cons_w1 = ifelse(ideology == "middle", ATTIDEO02_1, NA), 
      findout_ideo_opp_w1 = ifelse(ideology == "left" | ideology == "right", ATTIDEO02_2, NA), 
      findout_ind_cons_w1 = ifelse(ideology == "middle", ATTIDEO02_2, NA), 
      spendtime_ideo_opp_w1 = ifelse(ideology == "left" | ideology == "right", ATTIDEO02_3, NA), 
      spendtime_ind_cons_w1 = ifelse(ideology == "middle", ATTIDEO02_3, NA), 
      mean_ind_lib_w1 = ifelse(ideology == "middle", ATTIDEO1_IND_1, NA), 
      selfish_ind_lib_w1 = ifelse(ideology == "middle", ATTIDEO1_IND_2, NA), 
      stupid_ind_lib_w1 = ifelse(ideology == "middle", ATTIDEO1_IND_3, NA), 
      talk_ind_lib_w1 = ifelse(ideology == "middle", ATTIDEO2_IND_1, NA), 
      findout_ind_lib_w1 = ifelse(ideology == "middle", ATTIDEO2_IND_2, NA), 
      spend_ind_lib_w1 = ifelse(ideology == "middle", ATTIDEO2_IND_3, NA), 
      
      # Similarity
      similar_gun_w1 = ifelse(affiliation == "Democrat" | affiliation == "Republican", PERDIFF_1, NA), 
      similar_immigrant_w1 = ifelse(affiliation == "Democrat" | affiliation == "Republican", PERDIFF_2, NA),
      similar_climate_w1 = ifelse(affiliation == "Democrat" | affiliation == "Republican", PERDIFF_3, NA),
      similar_trump_w1 = ifelse(affiliation == "Democrat" | affiliation == "Republican", PERDIFF_4, NA),
      similar_econ_w1 = ifelse(affiliation == "Democrat" | affiliation == "Republican", PERDIFF_5, NA),
      similar_welfare_w1 = ifelse(affiliation == "Democrat" | affiliation == "Republican", 
                               ifelse(ISSUE == 3, PERDIFF_6, NA), 
                               NA),
      similar_health_w1 = ifelse(affiliation == "Democrat" | affiliation == "Republican", 
                              ifelse(ISSUE == 4, PERDIFF_6, NA), 
                              NA),
      similar_civil_w1 = ifelse(affiliation == "Democrat" | affiliation == "Republican", 
                             ifelse(ISSUE == 6, PERDIFF_6, NA), 
                             NA),
      similar_crime_w1 = ifelse(affiliation == "Democrat" | affiliation == "Republican", 
                             ifelse(ISSUE == 7, PERDIFF_6, NA), 
                             NA),
      similar_foreign_w1 = ifelse(affiliation == "Democrat" | affiliation == "Republican", 
                               ifelse(ISSUE == 8, PERDIFF_6, NA), 
                               NA),
      similar_supreme_w1 = ifelse(affiliation == "Democrat" | affiliation == "Republican", 
                               ifelse(ISSUE == 16, PERDIFF_6, NA), 
                               NA),
      similar_ind_dem_gun_w1= ifelse(affiliation == "Independent", PERDIFF_1, NA),
      similar_ind_dem_immigrant_w1 = ifelse(affiliation == "Independent", PERDIFF_2, NA),
      similar_ind_dem_climate_w1 = ifelse(affiliation == "Independent", PERDIFF_3, NA),
      similar_ind_dem_trump_w1 = ifelse(affiliation == "Independent", PERDIFF_4, NA),
      similar_ind_dem_econ_w1 = ifelse(affiliation == "Independent", PERDIFF_5, NA),
      similar_ind_dem_welfare_w1 = ifelse(affiliation == "Independent", 
                                        ifelse(ISSUE == 3, PERDIFF_6, NA),
                                        NA),
      similar_ind_dem_health_w1 = ifelse(affiliation == "Independent", 
                                       ifelse(ISSUE == 4, PERDIFF_6, NA),
                                       NA),
      similar_ind_dem_civil_w1 = ifelse(affiliation == "Independent", 
                                      ifelse(ISSUE == 6, PERDIFF_6, NA),
                                      NA),
      similar_ind_dem_crime_w1 = ifelse(affiliation == "Independent", 
                                      ifelse(ISSUE == 7, PERDIFF_6, NA),
                                      NA),
      similar_ind_dem_foreign_w1 = ifelse(affiliation == "Independent", 
                                        ifelse(ISSUE == 8, PERDIFF_6, NA),
                                        NA),
      similar_ind_dem_supreme_w1 = ifelse(affiliation == "Independent", 
                                        ifelse(ISSUE == 16, PERDIFF_6, NA),
                                        NA),
      similar_ind_rep_gun_w1 = ifelse(affiliation == "Independent", PERDIFF_IND_1, NA),
      similar_ind_rep_immigrant_w1 = ifelse(affiliation == "Independent", PERDIFF_IND_2, NA),
      similar_ind_rep_climate_w1 = ifelse(affiliation == "Independent", PERDIFF_IND_3, NA),
      similar_ind_rep_trump_w1 = ifelse(affiliation == "Independent", PERDIFF_IND_4, NA),
      similar_ind_rep_econ_w1 = ifelse(affiliation == "Independent", PERDIFF_IND_5, NA),
      similar_ind_rep_welfare_w1 = ifelse(affiliation == "Independent", 
                                         ifelse(ISSUE == 3, PERDIFF_IND_6, NA),
                                         NA),
      similar_ind_rep_health_w1 = ifelse(affiliation == "Independent", 
                                        ifelse(ISSUE == 4, PERDIFF_IND_6, NA),
                                        NA),
      similar_ind_rep_civil_w1 = ifelse(affiliation == "Independent", 
                                       ifelse(ISSUE == 6, PERDIFF_IND_6, NA),
                                       NA),
      similar_ind_rep_crime_w1 = ifelse(affiliation == "Independent", 
                                       ifelse(ISSUE == 7, PERDIFF_IND_6, NA),
                                       NA),
      similar_ind_rep_foreign_w1 = ifelse(affiliation == "Independent", 
                                         ifelse(ISSUE == 8, PERDIFF_IND_6, NA),
                                         NA),
      similar_ind_rep_supreme_w1 = ifelse(affiliation == "Independent", 
                                         ifelse(ISSUE == 16, PERDIFF_IND_6, NA),
                                         NA),
      # Selfish/mean/stupid
      mean_gun_w1 = case_when(
        rand_first == "Gun control" ~ ATTIS_EVAL_1_1,
        rand_second == "Gun control" ~ ATTIS_EVAL_2_1
      ),
      mean_immigrant_w1 = case_when(
        rand_first == "Immigration" ~ ATTIS_EVAL_1_1,
        rand_second == "Immigration" ~ ATTIS_EVAL_2_1
      ),
      mean_climate_w1 = case_when(
        rand_first == "Climate change" ~ ATTIS_EVAL_1_1,
        rand_second == "Climate change" ~ ATTIS_EVAL_2_1
      ),
      mean_trump_w1 = case_when(
        rand_first == "Presidency of Donald Trump" ~ ATTIS_EVAL_1_1,
        rand_second == "Presidency of Donald Trump" ~ ATTIS_EVAL_2_1
      ),
      mean_econ_w1 = case_when(
        rand_first == "Economy" ~ ATTIS_EVAL_1_1,
        rand_second == "Economy" ~ ATTIS_EVAL_2_1
      ),
      mean_welfare_w1 = ifelse(ISSUE == 3, ATTIS_EVAL_3_1, NA), 
      mean_health_w1 = ifelse(ISSUE == 4, ATTIS_EVAL_3_1, NA), 
      mean_civil_w1 = ifelse(ISSUE == 6, ATTIS_EVAL_3_1, NA), 
      mean_crime_w1 = ifelse(ISSUE == 7, ATTIS_EVAL_3_1, NA), 
      mean_foreign_w1 = ifelse(ISSUE == 8, ATTIS_EVAL_3_1, NA), 
      mean_supreme_w1 = ifelse(ISSUE == 16, ATTIS_EVAL_3_1, NA),
      selfish_gun_w1 = case_when(
        rand_first == "Gun control" ~ ATTIS_EVAL_1_2,
        rand_second == "Gun control" ~ ATTIS_EVAL_2_2
      ),
      selfish_immigrant_w1 = case_when(
        rand_first == "Immigration" ~ ATTIS_EVAL_1_2,
        rand_second == "Immigration" ~ ATTIS_EVAL_2_2
      ),
      selfish_climate_w1 = case_when(
        rand_first == "Climate change" ~ ATTIS_EVAL_1_2,
        rand_second == "Climate change" ~ ATTIS_EVAL_2_2
      ),
      selfish_trump_w1 = case_when(
        rand_first == "Presidency of Donald Trump" ~ ATTIS_EVAL_1_2,
        rand_second == "Presidency of Donald Trump" ~ ATTIS_EVAL_2_2
      ),
      selfish_econ_w1 = case_when(
        rand_first == "Economy" ~ ATTIS_EVAL_1_2,
        rand_second == "Economy" ~ ATTIS_EVAL_2_2
      ),
      selfish_welfare_w1 = ifelse(ISSUE == 3, ATTIS_EVAL_3_2, NA), 
      selfish_health_w1 = ifelse(ISSUE == 4, ATTIS_EVAL_3_2, NA), 
      selfish_civil_w1 = ifelse(ISSUE == 6, ATTIS_EVAL_3_2, NA), 
      selfish_crime_w1 = ifelse(ISSUE == 7, ATTIS_EVAL_3_2, NA), 
      selfish_foreign_w1 = ifelse(ISSUE == 8, ATTIS_EVAL_3_2, NA), 
      selfish_supreme_w1 = ifelse(ISSUE == 16, ATTIS_EVAL_3_2, NA),
      stupid_gun_w1 = case_when(
        rand_first == "Gun control" ~ ATTIS_EVAL_1_3,
        rand_second == "Gun control" ~ ATTIS_EVAL_2_3
      ),
      stupid_immigrant_w1 = case_when(
        rand_first == "Immigration" ~ ATTIS_EVAL_1_3,
        rand_second == "Immigration" ~ ATTIS_EVAL_2_3
      ),
      stupid_climate_w1 = case_when(
        rand_first == "Climate change" ~ ATTIS_EVAL_1_3,
        rand_second == "Climate change" ~ ATTIS_EVAL_2_3
      ),
      stupid_trump_w1 = case_when(
        rand_first == "Presidency of Donald Trump" ~ ATTIS_EVAL_1_3,
        rand_second == "Presidency of Donald Trump" ~ ATTIS_EVAL_2_3
      ),
      stupid_econ_w1 = case_when(
        rand_first == "Economy" ~ ATTIS_EVAL_1_3,
        rand_second == "Economy" ~ ATTIS_EVAL_2_3
      ),
      stupid_welfare_w1 = ifelse(ISSUE == 3, ATTIS_EVAL_3_3, NA), 
      stupid_health_w1 = ifelse(ISSUE == 4, ATTIS_EVAL_3_3, NA), 
      stupid_civil_w1 = ifelse(ISSUE == 6, ATTIS_EVAL_3_3, NA), 
      stupid_crime_w1 = ifelse(ISSUE == 7, ATTIS_EVAL_3_3, NA), 
      stupid_foreign_w1 = ifelse(ISSUE == 8, ATTIS_EVAL_3_3, NA), 
      stupid_supreme_w1 = ifelse(ISSUE == 16, ATTIS_EVAL_3_3, NA),
      
      # Attribution of malevolence - some flags:
      ## 1) some respondents had affiliation "unknown", which means that question was not displayed properly
      ## 2) "group" is empty for some; when affiliation was not empty, it is safe to assign whichever out of MALVOL_TREAT... or MALVOL_CTL... is not NA
      ## 3) a bunch of respondents have group == "control" but only have value on MALVOL_TREAT...; got NA for now
      norm_openness_w1 = ifelse(group == "treat", 
                                MALVOL_TREAT_10, MALVOL_CTL_6),
      malvol_1_w1 = ifelse(affiliation %in% c("Democrat", "Republican"),
                           ifelse(group %in% c("treat", ""), 
                                  MALVOL_TREAT_4, MALVOL_CTL_1), NA),
      malvol_2_w1 = ifelse(affiliation %in% c("Democrat", "Republican"),
                           ifelse(group %in% c("treat", ""), 
                                  MALVOL_TREAT_5, MALVOL_CTL_2), NA),
      malvol_3_w1 = ifelse(affiliation %in% c("Democrat", "Republican"),
                           ifelse(group %in% c("treat", ""), 
                                  MALVOL_TREAT_6, MALVOL_CTL_3), NA),
      malvol_4_w1 = ifelse(affiliation %in% c("Democrat", "Republican"),
                           ifelse(group %in% c("treat", ""), 
                                  MALVOL_TREAT_7, MALVOL_CTL_4), NA),
      malvol_5_w1 = ifelse(affiliation %in% c("Democrat", "Republican"),
                           ifelse(group %in% c("treat", ""), 
                                  MALVOL_TREAT_8, MALVOL_CTL_5), NA),
      malvol_ind_dem_1_w1 = ifelse(affiliation == "Independent",
                             ifelse(group == "treat", 
                                    MALVOL_TREAT_4, MALVOL_CTL_1), NA),
      malvol_ind_dem_2_w1 = ifelse(affiliation == "Independent",
                           ifelse(group == "treat", 
                                  MALVOL_TREAT_5, MALVOL_CTL_2), NA),
      malvol_ind_dem_3_w1 = ifelse(affiliation == "Independent",
                           ifelse(group == "treat", 
                                  MALVOL_TREAT_6, MALVOL_CTL_3), NA),
      malvol_ind_dem_4_w1 = ifelse(affiliation == "Independent",
                           ifelse(group == "treat", 
                                  MALVOL_TREAT_7, MALVOL_CTL_4), NA),
      malvol_ind_dem_5_w1 = ifelse(affiliation == "Independent",
                           ifelse(group == "treat", 
                                  MALVOL_TREAT_8, MALVOL_CTL_5), NA),
      
      # Perceived polarization
      norm_diversity_w1 = ifelse(group == "treat", 
                                 PERPOL_TREAT_1, PERPOL_CTL_5),
      perpol_1_w1 = ifelse(group == "treat", 
                           PERPOL_TREAT_2, PERPOL_CTL_1),
      perpol_2_w1 = ifelse(group == "treat", 
                           PERPOL_TREAT_3, PERPOL_CTL_2),
      perpol_3_w1 = ifelse(group == "treat", 
                           PERPOL_TREAT_4, PERPOL_CTL_3),
      perpol_4_w1 = ifelse(group == "treat", 
                           PERPOL_TREAT_5, PERPOL_CTL_4)) %>%
    
    # Drop the raw variables not needed 
    select(-c(ISSTRONG_6, ISPERSO_6, ISDISPERS_6, ISDISMEDIA_6, 
              ISDISONLINE_6, ISDISSOCIAL_6, ISDISMESSENGE_6, 
              GRRESPECT_6, GRUNDERSTAND_6, THERMO_13,
              ISDISPERS_1, ISDISPERS_2, ISDISPERS_3, 
              ISDISPERS_4, ISDISPERS_5,
              ISDISMEDIA_1, ISDISMEDIA_2, ISDISMEDIA_3, 
              ISDISMEDIA_4, ISDISMEDIA_5,
              ISDISONLINE_1, ISDISONLINE_2,ISDISONLINE_3, 
              ISDISONLINE_4, ISDISONLINE_5,
              ISDISSOCIAL_1, ISDISSOCIAL_2, ISDISSOCIAL_3, 
              ISDISSOCIAL_4, ISDISSOCIAL_5,
              ISDISMESSENGE_1, ISDISMESSENGE_2, ISDISMESSENGE_3, 
              ISDISMESSENGE_4, ISDISMESSENGE_5,
              GRRESPECT_7, GRRESPECT_8, GRRESPECT_9, GRRESPECT_10,
              GRUNDERSTAND_7, GRUNDERSTAND_8, GRUNDERSTAND_9, GRUNDERSTAND_10,
              PERDIFF_6,PERDIFF_IND_6,
              ATTIS_EVAL_1_1, ATTIS_EVAL_2_1, ATTIS_EVAL_3_1,
              ATTIS_EVAL_1_2, ATTIS_EVAL_2_2, ATTIS_EVAL_3_2,
              ATTIS_EVAL_1_3, ATTIS_EVAL_2_3, ATTIS_EVAL_3_3,
              ATTOTHER2_1, ATTOTHER2_2, ATTOTHER2_3,
              ATTOTHER3_1, ATTOTHER3_2, ATTOTHER3_3,
              ATTOTHER_2_IND_1, ATTOTHER_2_IND_2, ATTOTHER_2_IND_3,
              ATTOTHER_3_IND_1, ATTOTHER_3_IND_2, ATTOTHER_3_IND_3,
              PERDIFF_1, PERDIFF_2, PERDIFF_3, PERDIFF_4, PERDIFF_5, PERDIFF_6,
              PERDIFF_IND_1, PERDIFF_IND_2, PERDIFF_IND_3, 
              PERDIFF_IND_4, PERDIFF_IND_5, PERDIFF_IND_6,
              ATTIDEO1_1, ATTIDEO1_2, ATTIDEO1_3,
              ATTIDEO02_1, ATTIDEO02_2, ATTIDEO02_3,
              ATTIDEO1_IND_1, ATTIDEO1_IND_2, ATTIDEO1_IND_3,
              ATTIDEO2_IND_1, ATTIDEO2_IND_2, ATTIDEO2_IND_3,
              MALVOL_TREAT_10, MALVOL_TREAT_7, MALVOL_TREAT_8,
              MALVOL_TREAT_4, MALVOL_TREAT_5, MALVOL_TREAT_6,
              MALVOL_CTL_1, MALVOL_CTL_2, MALVOL_CTL_3,
              MALVOL_CTL_4, MALVOL_CTL_5, MALVOL_CTL_6,
              PERPOL_TREAT_1, PERPOL_TREAT_2, PERPOL_TREAT_3,
              PERPOL_TREAT_4, PERPOL_TREAT_5, PERPOL_CTL_1,
              PERPOL_CTL_2, PERPOL_CTL_3, PERPOL_CTL_4, PERPOL_CTL_5)) %>%
    
    # Rename variables according to Codebook
    rename(
      "start_date_us_w1" = StartDate,
      "end_date_us_w1" = EndDate,
      "meta_status_w1" = Status,
      "meta_progress_w1" = Progress,
      "meta_duration_w1" = Duration..in.seconds.,
      "meta_finished_w1" = Finished,
      "meta_recdate_w1" = RecordedDate,
      "meta_distribution_w1" = DistributionChannel,
      "meta_browser_w1" = META_Browser,
      "meta_version_w1" = META_Version,
      "meta_os_w1" = META_Operating.System,
      "meta_res_w1" = META_Resolution,
      "big5_1_w1" = BIG5_1,
      "big5_2_w1" = BIG5_2,
      "big5_3_w1" = BIG5_3,
      "big5_4_w1" = BIG5_4,
      "big5_5_w1" = BIG5_5,
      "big5_6_w1" = BIG5_6,
      "big5_7_w1" = BIG5_7,
      "big5_8_w1" = BIG5_8,
      "big5_9_w1" = BIG5_9,
      "big5_10_w1" = BIG5_10,
      "empathy_1_w1" = EMPATHY_1,
      "empathy_2_w1" = EMPATHY_2,
      "empathy_3_w1" = EMPATHY_3,
      "empathy_4_w1" = EMPATHY_4,
      "party_w1" = POL_BACK,
      "ideo_w1" = IDEO_BACKUP_1,
      "party_strength_1_w1" = PIDID_1,
      "party_strength_2_w1" = PIDID_2,
      "party_strength_3_w1" = PIDID_3,
      "party_strength_4_w1" = PIDID_4,
      "approve_reps_w1" = APREP,
      "approve_trump_w1" = APTRUMP,
      "days_week_tv_w1" = DAYSNEWS_1,
      "days_week_print_w1" = DAYSNEWS_2,
      "days_week_radio_w1" = DAYSNEWS_3,
      "days_week_web_w1" = DAYSNEWS_4,
      "days_week_sm_w1" = DAYSNEWS_5,
      "days_week_msg_w1" = DAYSNEWS_6,
      "days_week_f2f_w1" = DAYSNEWS_7,
      "days_week_app_w1" = DAYSNEWS_8,
      "days_week_search_w1" = DAYSNEWS_9,
      "print_other1_text_w1" = PRINT_20_TEXT,
      "print_other2_text_w1" = PRINT_21_TEXT,
      "print_other3_text_w1" = PRINT_22_TEXT,
      "print_days_nyt_w1" = PRINT_S_1,
      "print_days_wp_w1" = PRINT_S_2,
      "print_days_wsj_w1" = PRINT_S_3,
      "print_days_usat_w1" = PRINT_S_4,
      "print_days_chictri_w1" = PRINT_S_5,
      "print_days_latimes_w1" = PRINT_S_6,
      "print_days_metro_w1" = PRINT_S_7,
      "print_days_atlant_w1" = PRINT_S_8,
      "print_days_nyorker_w1" = PRINT_S_9,
      "print_days_econ_w1" = PRINT_S_10,
      "print_days_time_w1" = PRINT_S_11,
      "print_days_fortune_w1" = PRINT_S_12,
      "print_days_nation_w1" = PRINT_S_13,
      "print_days_spect_w1" = PRINT_S_14,
      "print_days_newsmax_w1" = PRINT_S_15,
      "print_days_amercons_w1" = PRINT_S_16,
      "print_days_weekstand_w1" = PRINT_S_17,
      "print_days_natrev_w1" = PRINT_S_18,
      "print_days_progress_w1" = PRINT_S_19,
      "print_days_other1_w1" = PRINT_S_20,
      "print_days_other2_w1" = PRINT_S_21,
      "print_days_other3_w1" = PRINT_S_22,
      "print_online_w1" = PRINT_ONOFF_1,
      "tv_other1_text_w1" = TV_8_TEXT,
      "tv_other2_text_w1" = TV_10_TEXT,
      "tv_other3_text_w1" = TV_11_TEXT,
      "tv_days_nbc_w1" = TV_S_1,
      "tv_days_cbs_w1" = TV_S_2,
      "tv_days_abc_w1" = TV_S_3,
      "tv_days_pbs_w1" = TV_S_4,
      "tv_days_fox_w1" = TV_S_5,
      "tv_days_msnbc_w1" = TV_S_6,
      "tv_days_cnn_w1" = TV_S_7,
      "tv_days_other1_w1" = TV_S_8,
      "tv_days_other2_w1" = TV_S_9,
      "tv_days_other3_w1" = TV_S_10,
      "tv_days_local_w1" = TV_S_11,
      "tv_online_w1" = TV_ONOFF_1,
      "radio_other1_text_w1" = RADIO_1_TEXT,
      "radio_other2_text_w1" = RADIO_2_TEXT,
      "radio_other3_text_w1" = RADIO_3_TEXT,
      "radio_other4_text_w1" = RADIO_4_TEXT,
      "radio_other5_text_w1" = RADIO_5_TEXT,
      "radio_days_other1_w1" = RADIO_S_1,
      "radio_days_other2_w1" = RADIO_S_2,
      "radio_days_other3_w1" = RADIO_S_3,
      "radio_days_other4_w1" = RADIO_S_4,
      "radio_days_other5_w1" = RADIO_S_5,
      "radio_online_w1" = RADIO_ONOFF_1,
      "webs_other1_text_w1" = WEBSITES_11_TEXT,
      "webs_other2_text_w1" = WEBSITES_13_TEXT,
      "webs_other3_text_w1" = WEBSITES_14_TEXT,
      "webs_days_huffpo_w1" = WEB_S_1,
      "webs_days_breitb_w1" = WEB_S_2,
      "webs_days_politico_w1" = WEB_S_3,
      "webs_days_yahoo_w1" = WEB_S_4,
      "webs_days_root_w1" = WEB_S_5,
      "webs_days_dbeast_w1" = WEB_S_6,
      "webs_days_slate_w1" = WEB_S_7,
      "webs_days_hill_w1" = WEB_S_8,
      "webs_days_buzzf_w1" = WEB_S_9,
      "webs_days_google_w1" = WEB_S_10,
      "webs_days_other1_w1" = WEB_S_11,
      "webs_days_other2_w1" = WEB_S_12,
      "webs_days_other3_w1" = WEB_S_13,
      "sm_other_text_w1" = SOCIAL_8_TEXT,
      "sm_days_twitter_w1" = SOCIAL_S_1,
      "sm_days_fbook_w1" = SOCIAL_S_2,
      "sm_days_ytube_w1" = SOCIAL_S_3,
      "sm_days_insta_w1" = SOCIAL_S_4,
      "sm_days_snapch_w1" = SOCIAL_S_5,
      "sm_days_tumblr_w1" = SOCIAL_S_6,
      "sm_days_reddit_w1" = SOCIAL_S_7,
      "sm_days_other_w1" = SOCIAL_S_8,
      "msg_others_text_w1" = MESSAGING_7_TEXT,
      "msg_days_whats_w1" = MESSAGING_S_1,
      "msg_days_viber_w1" = MESSAGING_S_2,
      "msg_days_telegr_w1" = MESSAGING_S_3,
      "msg_days_snapch_w1" = MESSAGING_S_4,
      "msg_days_line_w1" = MESSAGING_S_5,
      "msg_days_hangouts_w1" = MESSAGING_S_6,
      "msg_days_fbmess_w1" = MESSAGING_S_7,
      "msg_days_others_w1" = MESSAGING_S_8,
      "mob_aggreg_text_w1" = MOBILE_1_TEXT,
      "mob_others_text_w1" = MOBILE_14_TEXT,
      "mob_days_aggreg_w1" = MOBILE_S_1,
      "mob_days_applenews_w1" = MOBILE_S_2,
      "mob_days_googlenews_w1" = MOBILE_S_3,
      "mob_days_wp_w1" = MOBILE_S_4,
      "mob_days_nyt_w1" = MOBILE_S_5,
      "mob_days_cnn_w1" = MOBILE_S_6,
      "mob_days_wsj_w1" = MOBILE_S_7,
      "mob_days_npr_w1" = MOBILE_S_8,
      "mob_days_ap_w1" = MOBILE_S_9,
      "mob_days_atlant_w1" = MOBILE_S_10,
      "mob_days_huffpo_w1" = MOBILE_S_11,
      "mob_days_root_w1" = MOBILE_S_12,
      "mob_days_dbeast_w1" = MOBILE_S_13,
      "mob_days_others_w1" = MOBILE_S_14,
      "pers_issue_w1" = ISSUE,
      "attent_w1" = ATTN,
      "attent_text_w1" = ATTN_6_TEXT,
      "att_econ_1_w1" = ECON_1,
      "att_econ_2_w1" = ECON_2,
      "att_econ_3_w1" = ECON_3,
      "att_welfare_1_w1" = SOCSEC_1,
      "att_welfare_2_w1" = SOCSEC_2,
      "att_welfare_3_w1" = SOCSEC_3,
      "att_health_1_w1" = HEALTH_1,
      "att_health_2_w1" = HEALTH_2,
      "att_health_3_w1" = HEALTH_3,
      "att_civil_1_w1" = RACEREL_1,
      "att_civil_2_w1" = RACEREL_2,
      "att_civil_3_w1" = RACEREL_3,
      "att_civil_4_w1" = RACEREL_4,
      "att_civil_5_w1" = RACEREL_5,
      "att_civil_6_w1" = RACEREL_6,
      "att_crime_1_w1" = CRIME_1,
      "att_crime_2_w1" = CRIME_2,
      "att_crime_3_w1" = CRIME_3,
      "att_foreign_1_w1" = INTER_1,
      "att_foreign_2_w1" = INTER_2,
      "att_foreign_3_w1" = INTER_3,
      "att_climate_1_w1" = ENV_1,
      "att_climate_2_w1" = ENV_2,
      "att_climate_3_w1" = ENV_3,
      "att_immigrant_1_w1" = IMMIG_1,
      "att_immigrant_2_w1" = IMMIG_2,
      "att_immigrant_3_w1" = IMMIG_3,
      "att_gun_1_w1" = GUN_1,
      "att_gun_2_w1" = GUN_2,
      "att_gun_3_w1" = GUN_3,
      "att_supreme_1_w1" = SUPREME_1,
      "att_supreme_2_w1" = SUPREME_2,
      "att_supreme_3_w1" = SUPREME_3,
      "att_trump_1_w1" = TRUMP_1,
      "att_trump_2_w1" = TRUMP_2,
      "att_trump_3_w1" = TRUMP_3,
      "strong_gun_w1" = ISSTRONG_1,
      "strong_immigrant_w1" = ISSTRONG_2,
      "strong_climate_w1" = ISSTRONG_3,
      "strong_trump_w1" = ISSTRONG_4,
      "strong_econ_w1" = ISSTRONG_5,
      "important_gun_w1" = ISPERSO_1,
      "important_immigrant_w1" = ISPERSO_2,
      "important_climate_w1" = ISPERSO_3,
      "important_trump_w1" = ISPERSO_4,
      "important_econ_w1" = ISPERSO_5,
      "respect_gun_w1" = GRRESPECT_1,
      "respect_immigrant_w1" = GRRESPECT_2,
      "respect_climate_w1" = GRRESPECT_3,
      "respect_trump_w1" = GRRESPECT_4,
      "respect_econ_w1" = GRRESPECT_5,
      "understand_gun_w1" = GRUNDERSTAND_1,
      "understand_immigrant_w1" = GRUNDERSTAND_2,
      "understand_climate_w1" = GRUNDERSTAND_3,
      "understand_trump_w1" = GRUNDERSTAND_4,
      "understand_econ_w1" = GRUNDERSTAND_5,
      "ft_dem_supporters_w1" = THERMO_4,
      "ft_rep_supporters_w1" = THERMO_5,
      "ft_liberals_w1" = THERMO_6,
      "ft_conservatives_w1" = THERMO_7,
      "ft_dem_party_w1" = THERMO_17,
      "ft_rep_party_w1" = THERMO_18,
      "ft_gun_w1" = THERMO_9,
      "ft_immigrant_w1" = THERMO_10,
      "ft_climate_w1" = THERMO_11,
      "ft_econ_w1" = THERMO_12,
      "ft_trump_w1" = THERMO_16,
      "misinfo_1_w1" = FALSE_1,
      "misinfo_2_w1" = FALSE_2,
      "misinfo_3_w1" = FALSE_3,
      "misinfo_4_w1" = FALSE_4,
      "malvol_ind_rep_1_w1" = MALVOL_IND_1,
      "malvol_ind_rep_2_w1" = MALVOL_IND_2,
      "malvol_ind_rep_3_w1" = MALVOL_IND_3,
      "malvol_ind_rep_4_w1" = MALVOL_IND_4,
      "malvol_ind_rep_5_w1" = MALVOL_IND_5,
      "percent_this_comp_w1" = DEVICE_1,
      "percent_other_comp_w1" = DEVICE_4,
      "percent_phone_w1" = DEVICE_2,
      "percent_tablet_w1" = DEVICE_3,
      "survey_interested_w1" = Q240,
      "survey_comments_w1" = Q249,
      "embed_affiliation_w1" = affiliation,
      "embed_randomizer_w1" = randomizer,
      "embed_ideo_w1" = ideology,
      "norm_treatment_w1" = group,
      "embed_ideo_label_w1" = ideo_label,
      "embed_party_label_w1" = affil_party,
      "embed_opparty_w1" = opposite,
      "embed_opparty_label_w1" = op_party,
      "embed_opparty2_w1" = opposite_2,
      "embed_opparty2_label_w1" = op_party_2,
      "embed_opideo_w1" = op_ideology,
      "embed_opideo_label_w1" = op_ideo_label,
      "embed_opideo2_w1" = op_ideology_2,
      "embed_rando1_w1" = rand_first,
      "embed_rando2_w1" = rand_second,
    ) %>%
    # Extract outlet-level exposure variables
    mutate(print_nyt_w1 = ifelse(grepl("^1,|^1$", .$PRINT), 1, 0),
           print_wp_w1 = ifelse(grepl("2,|,2$|^2$", .$PRINT), 1, 0),
           print_wsj_w1 = ifelse(grepl(",3|^3", .$PRINT), 1, 0),
           print_usat_w1 = ifelse(grepl(",4|^4", .$PRINT), 1, 0),
           print_chictri_w1 = ifelse(grepl(",5|^5", .$PRINT), 1, 0),
           print_latimes_w1 = ifelse(grepl(",6|^6", .$PRINT), 1, 0),
           print_metro_w1 = ifelse(grepl(",7|^7", .$PRINT), 1, 0),
           print_atlant_w1 = ifelse(grepl(",8|^8", .$PRINT), 1, 0),
           print_nyorker_w1 = ifelse(grepl(",9|^9", .$PRINT), 1, 0),
           print_econ_w1 = ifelse(grepl("10", .$PRINT), 1, 0),
           print_time_w1 = ifelse(grepl("11", .$PRINT), 1, 0),
           print_fortune_w1 = ifelse(grepl("12", .$PRINT), 1, 0),
           print_nation_w1 = ifelse(grepl("13", .$PRINT), 1, 0),
           print_spect_w1 = ifelse(grepl("14", .$PRINT), 1, 0),
           print_newsmax_w1 = ifelse(grepl("15", .$PRINT), 1, 0),
           print_amercons_w1 = ifelse(grepl("16", .$PRINT), 1, 0),
           print_weekstand_w1 = ifelse(grepl("17", .$PRINT), 1, 0),
           print_natrev_w1 = ifelse(grepl("18", .$PRINT), 1, 0),
           print_progress_w1 = ifelse(grepl("19", .$PRINT), 1, 0),
           print_other1_w1 = ifelse(grepl("20", .$PRINT), 1, 0),
           print_other2_w1 = ifelse(grepl("21", .$PRINT), 1, 0),
           print_other3_w1 = ifelse(grepl("22", .$PRINT), 1, 0)) %>%
    mutate(webs_huffpo_w1 = ifelse(grepl("^1,|^1$", .$WEBSITES), 1, 0),
           webs_breitb_w1 = ifelse(grepl("2,|,2$|^2$", .$WEBSITES), 1, 0),
           webs_politico_w1 = ifelse(grepl(",3|^3", .$WEBSITES), 1, 0),
           webs_yahoo_w1 = ifelse(grepl(",4|^4", .$WEBSITES), 1, 0),
           webs_root_w1 = ifelse(grepl(",5|^5", .$WEBSITES), 1, 0),
           webs_dbeast_w1 = ifelse(grepl(",6|^6", .$WEBSITES), 1, 0),
           webs_slate_w1 = ifelse(grepl(",7|^7", .$WEBSITES), 1, 0),
           webs_hill_w1 = ifelse(grepl(",8|^8", .$WEBSITES), 1, 0),
           webs_buzzf_w1 = ifelse(grepl(",9|^9", .$WEBSITES), 1, 0),
           webs_google_w1 = ifelse(grepl("10", .$WEBSITES), 1, 0),
           webs_other1_w1 = ifelse(grepl("11", .$WEBSITES), 1, 0),
           webs_other2_w1 = ifelse(grepl("13", .$WEBSITES), 1, 0),
           webs_other3_w1 = ifelse(grepl("14", .$WEBSITES), 1, 0)) %>%
    mutate(tv_nbc_w1 = ifelse(grepl("^1,|^1$", .$TV), 1, 0),
           tv_cbs_w1 = ifelse(grepl("2,|,2$|^2$", .$TV), 1, 0),
           tv_abc_w1 = ifelse(grepl(",3|^3", .$TV), 1, 0),
           tv_pbs_w1 = ifelse(grepl(",4|^4", .$TV), 1, 0),
           tv_fox_w1 = ifelse(grepl(",5|^5", .$TV), 1, 0),
           tv_msnbc_w1 = ifelse(grepl(",6|^6", .$TV), 1, 0),
           tv_cnn_w1 = ifelse(grepl(",7|^7", .$TV), 1, 0),
           tv_other1_w1 = ifelse(grepl(",8|^8", .$TV), 1, 0),
           tv_local_w1 = ifelse(grepl(",9|^9", .$TV), 1, 0),
           tv_other2_w1 = ifelse(grepl("10", .$TV), 1, 0),
           tv_other3_w1 = ifelse(grepl("11", .$TV), 1, 0)) %>%
    mutate(radio_other1_w1 = ifelse(grepl("^1,|^1$", .$RADIO), 1, 0),
           radio_other2_w1 = ifelse(grepl("2,|,2$|^2$", .$RADIO), 1, 0),
           radio_other3_w1 = ifelse(grepl(",3|^3", .$RADIO), 1, 0),
           radio_other4_w1 = ifelse(grepl(",4|^4", .$RADIO), 1, 0),
           radio_other5_w1 = ifelse(grepl(",5|^5", .$RADIO), 1, 0)) %>%
    mutate(sm_twitter_w1 = ifelse(grepl("^1,|^1$", .$SOCIAL), 1, 0),
           sm_fbook_w1 = ifelse(grepl("2,|,2$|^2$", .$SOCIAL), 1, 0),
           sm_ytube_w1 = ifelse(grepl(",3|^3", .$SOCIAL), 1, 0),
           sm_insta_w1 = ifelse(grepl(",4|^4", .$SOCIAL), 1, 0),
           sm_snapch_w1 = ifelse(grepl(",5|^5", .$SOCIAL), 1, 0),
           sm_tumblr_w1 = ifelse(grepl(",6|^6", .$SOCIAL), 1, 0),
           sm_reddit_w1 = ifelse(grepl(",7|^7", .$SOCIAL), 1, 0),
           sm_other_w1 = ifelse(grepl(",8|^8", .$SOCIAL), 1, 0))  %>%
    mutate(msg_whats_w1 = ifelse(grepl("^1,|^1$", .$MESSAGING), 1, 0),
           msg_viber_w1 = ifelse(grepl("2,|,2$|^2$", .$MESSAGING), 1, 0),
           msg_telegr_w1 = ifelse(grepl(",3|^3", .$MESSAGING), 1, 0),
           msg_snapch_w1 = ifelse(grepl(",4|^4", .$MESSAGING), 1, 0),
           msg_line_w1 = ifelse(grepl(",5|^5", .$MESSAGING), 1, 0),
           msg_hangouts_w1 = ifelse(grepl(",6|^6", .$MESSAGING), 1, 0),
           msg_others_w1 = ifelse(grepl(",7|^7", .$MESSAGING), 1, 0),
           msg_fbmess_w1 = ifelse(grepl(",8|^8", .$MESSAGING), 1, 0)) %>%
    mutate(mob_aggreg_w1 = ifelse(grepl("^1,|^1$", .$MOBILE), 1, 0),
           mob_applenews_w1 = ifelse(grepl("2,|,2$|^2$", .$MOBILE), 1, 0),
           mob_googlenews_w1 = ifelse(grepl(",3|^3", .$MOBILE), 1, 0),
           mob_wp_w1 = ifelse(grepl(",4|^4", .$MOBILE), 1, 0),
           mob_nyt_w1 = ifelse(grepl(",5|^5", .$MOBILE), 1, 0),
           mob_cnn_w1 = ifelse(grepl(",6|^6", .$MOBILE), 1, 0),
           mob_wsj_w1 = ifelse(grepl(",7|^7", .$MOBILE), 1, 0),
           mob_npr_w1 = ifelse(grepl(",8|^8", .$MOBILE), 1, 0),
           mob_ap_w1 = ifelse(grepl(",9|^9", .$WEBSITES), 1, 0),
           mob_atlant_w1 = ifelse(grepl("10", .$WEBSITES), 1, 0),
           mob_huffpo_w1 = ifelse(grepl("11", .$WEBSITES), 1, 0),
           mob_root_w1 = ifelse(grepl("12", .$WEBSITES), 1, 0),
           mob_dbeast_w1 = ifelse(grepl("13", .$WEBSITES), 1, 0),
           mob_others_w1 = ifelse(grepl("13", .$WEBSITES), 1, 0)
           ) %>%
    # Recode some scales where natural end should be 0, not 1
    mutate(across(starts_with("days_week"), ~ .x - 1),
           across(starts_with("print_days"), ~ .x - 1),
           across(starts_with("tv_days"), ~ .x - 1),
           across(starts_with("radio_days"), ~ .x - 1),
           across(starts_with("webs_days"), ~ .x - 1),
           across(starts_with("sm_days"), ~ .x - 1),
           across(starts_with("msg_days"), ~ .x - 1),
           across(starts_with("mob_days"), ~ .x - 1)) %>%
    # Misinformation
    mutate(across(starts_with("misinfo_"), ~case_when(
      . == 3 ~ 4,
      . == 4 ~ 5,
      . == 5 ~ 3,
      TRUE ~ as.numeric(.)))) %>%
    select(-c(TV, PRINT, RADIO, WEBSITES, SOCIAL, MOBILE, MESSAGING)) %>%
    
    # Dates
    mutate(start_date_us_w1 = as.POSIXct(
      start_date_us_w1, format = "%Y-%m-%d %H:%M:%S", tz = "MST"),
      end_date_us_w1 = as.POSIXct(
        end_date_us_w1, format = "%Y-%m-%d %H:%M:%S", tz = "MST"))
  
  # For some early respondents, the party piping did not work.
  # (column "party_questions" in people_surveys_dates_us_all)
  # Hence, recode all questions where that pipe was used to NA
  
  party_people <- c('R_116n78ZqVoZJZJA','R_12hTG8Tjc832z6Q','R_1DAvoKcZo6xcshs',
                   'R_1eWZvJGqiK9OcVi','R_1FS7dDubElxiWQW','R_1gpb5SHe6ju5jUj',
                   'R_1hZo3aurqP6gyHE','R_1Iz4yRaxmK0f2na','R_1MMvut1oVlMY4Em',
                   'R_1mVFb6vieWEM7vH','R_1mxTg6MjlGMszni','R_1pEFyeaHpcTzWMH',
                   'R_1rqV866GfMydFIo','R_1T663txMbCKzbH3','R_24VAE8c7PUv5UHf',
                   'R_27J3cnaRuDQ2yK9','R_27J7AiNgPxIUAA9','R_27qdhcxVrFR6Lfl',
                   'R_2aFuO4rDK3uORtD','R_2dA35NRZBQDOc2s','R_2dKFFi4OGXBPHYD',
                   'R_2EhJKSqTur1jkXC','R_2h5ZdJlbMQm1aIV','R_2PuLHKmTBaZdiZ3',
                   'R_2t3ScTJWEGCJ6Kp','R_2taMYnEL1jS1DZN','R_2UhgNf8IxZ2ArSz',
                   'R_2v8QR12YbNR9oxg','R_2vdHe0D62xjvl70','R_2VfHKEcluAWeA7Y',
                   'R_2vjj6rHIRxcOYAO','R_306UlWK1nOOCwg8','R_31AH8iBehPPrygr',
                   'R_31HhkoQ5KoqjlJ1','R_3EasZli2itX27iO','R_3F2sMjDqBxgkVOu',
                   'R_3G7XFyViPT0hud7','R_3GjoUfG4VNqWaOI','R_3J8x6rWZwBWnq5M',
                   'R_3JK1AB7TE75Te80','R_3MbO2DsVULcbZzn','R_3oHDmrdx1c0yz9n',
                   'R_3pbgf0A1V25qpcg','R_3pqWvP6L4MJscjC','R_3r25Yw8w3Cx3cYf',
                   'R_4OQ1TCZPPKoYnkd','R_88Od8E2YuK81EHv','R_AcblFtGKvwrBWdb',
                   'R_bkKOZAkSJWwASf7','R_d0ZrgO2nvla8jcJ','R_Oxr6nBpWgJyWUfL',
                   'R_PUMxRHWFUhJuCat','R_RwfyrgUCu3oQgO5','R_SDDyZE6BHRVMOc1',
                   'R_T0lz5McHJjP7BD3','R_T4X3XoXWZIhIRBD','R_urdsTCpKtttHgBj',
                   'R_wX0DlRvlQ46d7u9','R_1mXIihb6yNRY5co','R_1d19JyVFcQYrpqz',
                   'R_4HZFOfPgM2tPZeh','R_3Kvz051f7SOSQsH','R_25NiCPngKTWj94g',
                   'R_0PbPhvzFphu8n7j','R_bIdECfh7iDy6v7z','R_UAST1MZshCS3B05',
                   'R_2YWC9tDQgQuKt7W','R_28GNs5TNy3PtSVR','R_1gduRVjildTqh7y',
                   'R_2U2TiklXoKbh35d','R_2wvHtyxJPeBHCLE','R_ReLTlEzTtx9xCM1',
                   'R_BxLa54RTBCtCtKF','R_1E4F37UYH77UVjJ','R_3ltPygDEaDH3knF',
                   'R_1DZHhNcavDpiCw3','R_3en3KENWZDQECGn','R_23eF3IpQk89X7gs',
                   'R_24vkbpNH4KVlajQ','R_3FW1Qjfi9a6APZD','R_1kHsJWEuuukVQGM')
  
  data <- data %>% 
    mutate(across(c(party_strength_1_w1, party_strength_2_w1,
                  party_strength_3_w1, party_strength_4_w1,
                  respect_party_opp_w1, respect_ind_rep_w1, 
                  respect_ideo_opp_w1,
                  understand_party_opp_w1, understand_ind_rep_w1,
                  understand_ideo_opp_w1, understand_ind_lib_w1,
                  mean_party_opp_w1, mean_ind_dem_w1, 
                  selfish_party_opp_w1, selfish_ind_dem_w1, 
                  stupid_party_opp_w1, stupid_ind_dem_w1,
                  talk_party_opp_w1, talk_ind_dem_w1,
                  findout_party_opp_w1, similar_ind_rep_gun_w1, 
                  similar_ind_rep_immigrant_w1, similar_ind_rep_climate_w1,
                  similar_ind_rep_trump_w1, similar_ind_rep_econ_w1,
                  similar_ind_rep_welfare_w1, similar_ind_rep_health_w1, 
                  similar_ind_rep_civil_w1, similar_ind_rep_crime_w1, 
                  similar_ind_rep_foreign_w1, similar_ind_rep_supreme_w1,
                  malvol_1_w1, malvol_ind_dem_1_w1,malvol_2_w1,
                  malvol_ind_dem_2_w1, malvol_3_w1, malvol_ind_dem_3_w1,
                  malvol_4_w1, malvol_ind_dem_4_w1, malvol_5_w1,
                  malvol_ind_dem_5_w1, malvol_1_w1, malvol_ind_dem_1_w1,
                  malvol_2_w1, malvol_ind_dem_2_w1, malvol_3_w1,
                  malvol_ind_dem_3_w1, malvol_4_w1, malvol_ind_dem_4_w1,
                  malvol_5_w1, malvol_ind_dem_5_w1, norm_openness_w1,
                  malvol_ind_rep_1_w1, malvol_ind_rep_2_w1, malvol_ind_rep_3_w1,
                  malvol_ind_rep_4_w1, malvol_ind_rep_5_w1, findout_ind_dem_w1,
                  spendtime_party_opp_w1, spendtime_ind_dem_w1, mean_ind_rep_w1,
                  selfish_ind_rep_w1, stupid_ind_rep_w1, talk_ind_rep_w1,
                  findout_ind_rep_w1, spendtime_ind_rep_w1,
                  respect_ind_dem_w1, understand_ind_dem_w1, understand_ind_cons_w1),
           ~ifelse(ResponseId_w1 %in% party_people, NA, .)))
  
  return(data)
}

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# W2 recoding ####
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  
recode_US_w2 <- function(data) {
  
  # data <- data %>%
    
  data <- data %>%
    mutate(

      # Take care of some Qualtrics coding errors
      ISPERSO_1 = case_when(
        ISPERSO_1 == 8 ~ 6,
        ISPERSO_1 == 9 ~ 7,
        TRUE ~ as.numeric(ISPERSO_1)
      ),
      ISPERSO_2 = case_when(
        ISPERSO_2 == 8 ~ 6,
        ISPERSO_2 == 9 ~ 7,
        TRUE ~ as.numeric(ISPERSO_2)
      ),
      ISPERSO_3 = case_when(
        ISPERSO_3 == 8 ~ 6,
        ISPERSO_3 == 9 ~ 7,
        TRUE ~ as.numeric(ISPERSO_3)
      ),
      ISPERSO_4 = case_when(
        ISPERSO_4 == 8 ~ 6,
        ISPERSO_4 == 9 ~ 7,
        TRUE ~ as.numeric(ISPERSO_4)
      ),
      ISPERSO_5 = case_when(
        ISPERSO_5 == 8 ~ 6,
        ISPERSO_5 == 9 ~ 7,
        TRUE ~ as.numeric(ISPERSO_5)
      ),
      ISPERSO_6 = case_when(
        ISPERSO_6 == 8 ~ 6,
        ISPERSO_6 == 9 ~ 7,
        TRUE ~ as.numeric(ISPERSO_6)
      ),
      ECON_1 = case_when(
        ECON_1 == 101 ~ 8,
        ECON_1 == 102 ~ 9,
        ECON_1 == 103 ~ 10,
        ECON_1 == 104 ~ 11,
        ECON_1 == 105 ~ 12,
        ECON_1 == 106 ~ 13,
        TRUE ~ as.numeric(ECON_1)
      ),
      ECON_2 = case_when(
        ECON_2 == 101 ~ 8,
        ECON_2 == 102 ~ 9,
        ECON_2 == 103 ~ 10,
        ECON_2 == 104 ~ 11,
        ECON_2 == 105 ~ 12,
        ECON_2 == 106 ~ 13,
        TRUE ~ as.numeric(ECON_2)
      ),
      ECON_3 = case_when(
        ECON_3 == 101 ~ 8,
        ECON_3 == 102 ~ 9,
        ECON_3 == 103 ~ 10,
        ECON_3 == 104 ~ 11,
        ECON_3 == 105 ~ 12,
        ECON_3 == 106 ~ 13,
        TRUE ~ as.numeric(ECON_3)
      )
    ) %>%
    # Extract personally important issue variables
    mutate(
      ## Issue strength
      strong_welfare_w2 = ifelse(issue_txt == "Social services", ISSTRONG_6, NA),
      strong_health_w2 = ifelse(issue_txt == "Healthcare", ISSTRONG_6, NA),
      strong_civil_w2 = ifelse(issue_txt == "Civil rights issues", ISSTRONG_6, NA),
      strong_crime_w2 = ifelse(issue_txt == "Law and justice", ISSTRONG_6, NA),
      strong_foreign_w2 = ifelse(issue_txt == "Foreign affairs", ISSTRONG_6, NA),
      strong_supreme_w2 = ifelse(issue_txt == "The Supreme Court", ISSTRONG_6, NA),
      ## Issue relevance
      important_welfare_w2 = ifelse(issue_txt == "Social services", ISPERSO_6, NA),
      important_health_w2 = ifelse(issue_txt == "Healthcare", ISPERSO_6, NA),
      important_civil_w2 = ifelse(issue_txt == "Civil rights issues", ISPERSO_6, NA),
      important_crime_w2 = ifelse(issue_txt == "Law and justice", ISPERSO_6, NA),
      important_foreign_w2 = ifelse(issue_txt == "Foreign affairs", ISPERSO_6, NA),
      important_supreme_w2 = ifelse(issue_txt == "The Supreme Court", ISPERSO_6, NA),
      ## Understanding for people with opposite opinion
      understand_welfare_w2 = ifelse(issue_txt == "Social services", GRUNDERSTAND_6, NA),
      understand_health_w2 = ifelse(issue_txt == "Healthcare", GRUNDERSTAND_6, NA),
      understand_civil_w2 = ifelse(issue_txt == "Civil rights issues", GRUNDERSTAND_6, NA),
      understand_crime_w2 = ifelse(issue_txt == "Law and justice", GRUNDERSTAND_6, NA),
      understand_foreign_w2 = ifelse(issue_txt == "Foreign affairs", GRUNDERSTAND_6, NA),
      understand_supreme_w2 = ifelse(issue_txt == "The Supreme Court", GRUNDERSTAND_6, NA),
      ## FT for people with opposite opinion
      ft_welfare_w2 = ifelse(issue_txt == "Social services", THERMO_13, NA),
      ft_health_w2 = ifelse(issue_txt == "Healthcare", THERMO_13, NA),
      ft_civil_w2 = ifelse(issue_txt == "Civil rights issues", THERMO_13, NA),
      ft_crime_w2 = ifelse(issue_txt == "Law and justice", THERMO_13, NA),
      ft_foreign_w2 = ifelse(issue_txt == "Foreign affairs", THERMO_13, NA),
      ft_supreme_w2 = ifelse(issue_txt == "The Supreme Court", THERMO_13, NA)
    ) %>%
    # Selfish/mean/stupid
    mutate(
      mean_gun_w2 = case_when(
        rand_first == "Gun control" ~ ATTIS_EVAL_1_1,
        rand_second == "Gun control" ~ ATTIS_EVAL_2_1
      ),
      mean_immigrant_w2 = case_when(
        rand_first == "Immigration" ~ ATTIS_EVAL_1_1,
        rand_second == "Immigration" ~ ATTIS_EVAL_2_1
      ),
      mean_climate_w2 = case_when(
        rand_first == "Climate change" ~ ATTIS_EVAL_1_1,
        rand_second == "Climate change" ~ ATTIS_EVAL_2_1
      ),
      mean_trump_w2 = case_when(
        rand_first == "Presidency of Donald Trump" ~ ATTIS_EVAL_1_1,
        rand_second == "Presidency of Donald Trump" ~ ATTIS_EVAL_2_1
      ),
      mean_econ_w2 = case_when(
        rand_first == "Economy" ~ ATTIS_EVAL_1_1,
        rand_second == "Economy" ~ ATTIS_EVAL_2_1
      ),
      mean_welfare_w2 = ifelse(issue_txt == "Social services", ATTIS_EVAL_3_1, NA),
      mean_health_w2 = ifelse(issue_txt == "Healthcare", ATTIS_EVAL_3_1, NA),
      mean_civil_w2 = ifelse(issue_txt == "Civil rights issues", ATTIS_EVAL_3_1, NA),
      mean_crime_w2 = ifelse(issue_txt == "Law and justice", ATTIS_EVAL_3_1, NA),
      mean_foreign_w2 = ifelse(issue_txt == "Foreign affairs", ATTIS_EVAL_3_1, NA),
      mean_supreme_w2 = ifelse(issue_txt == "The Supreme Court", ATTIS_EVAL_3_1, NA),
      selfish_gun_w2 = case_when(
        rand_first == "Gun control" ~ ATTIS_EVAL_1_2,
        rand_second == "Gun control" ~ ATTIS_EVAL_2_2
      ),
      selfish_immigrant_w2 = case_when(
        rand_first == "Immigration" ~ ATTIS_EVAL_1_2,
        rand_second == "Immigration" ~ ATTIS_EVAL_2_2
      ),
      selfish_climate_w2 = case_when(
        rand_first == "Climate change" ~ ATTIS_EVAL_1_2,
        rand_second == "Climate change" ~ ATTIS_EVAL_2_2
      ),
      selfish_trump_w2 = case_when(
        rand_first == "Presidency of Donald Trump" ~ ATTIS_EVAL_1_2,
        rand_second == "Presidency of Donald Trump" ~ ATTIS_EVAL_2_2
      ),
      selfish_econ_w2 = case_when(
        rand_first == "Economy" ~ ATTIS_EVAL_1_2,
        rand_second == "Economy" ~ ATTIS_EVAL_2_2
      ),
      selfish_welfare_w2 = ifelse(issue_txt == "Social services", ATTIS_EVAL_3_2, NA),
      selfish_health_w2 = ifelse(issue_txt == "Healthcare", ATTIS_EVAL_3_2, NA),
      selfish_civil_w2 = ifelse(issue_txt == "Civil rights issues", ATTIS_EVAL_3_2, NA),
      selfish_crime_w2 = ifelse(issue_txt == "Law and justice", ATTIS_EVAL_3_2, NA),
      selfish_foreign_w2 = ifelse(issue_txt == "Foreign affairs", ATTIS_EVAL_3_2, NA),
      selfish_supreme_w2 = ifelse(issue_txt == "The Supreme Court", ATTIS_EVAL_3_2, NA),
      stupid_gun_w2 = case_when(
        rand_first == "Gun control" ~ ATTIS_EVAL_1_3,
        rand_second == "Gun control" ~ ATTIS_EVAL_2_3
      ),
      stupid_immigrant_w2 = case_when(
        rand_first == "Immigration" ~ ATTIS_EVAL_1_3,
        rand_second == "Immigration" ~ ATTIS_EVAL_2_3
      ),
      stupid_climate_w2 = case_when(
        rand_first == "Climate change" ~ ATTIS_EVAL_1_3,
        rand_second == "Climate change" ~ ATTIS_EVAL_2_3
      ),
      stupid_trump_w2 = case_when(
        rand_first == "Presidency of Donald Trump" ~ ATTIS_EVAL_1_3,
        rand_second == "Presidency of Donald Trump" ~ ATTIS_EVAL_2_3
      ),
      stupid_econ_w2 = case_when(
        rand_first == "Economy" ~ ATTIS_EVAL_1_3,
        rand_second == "Economy" ~ ATTIS_EVAL_2_3
      ),
      stupid_welfare_w2 = ifelse(issue_txt == "Social services", ATTIS_EVAL_3_3, NA),
      stupid_health_w2 = ifelse(issue_txt == "Healthcare", ATTIS_EVAL_3_3, NA),
      stupid_civil_w2 = ifelse(issue_txt == "Civil rights issues", ATTIS_EVAL_3_3, NA),
      stupid_crime_w2 = ifelse(issue_txt == "Law and justice", ATTIS_EVAL_3_3, NA),
      stupid_foreign_w2 = ifelse(issue_txt == "Foreign affairs", ATTIS_EVAL_3_3, NA),
      stupid_supreme_w2 = ifelse(issue_txt == "The Supreme Court", ATTIS_EVAL_3_3, NA),
      # Perceived issue difference
      similar_welfare_w2 = ifelse(issue_txt == "Social services", PERDIFF_6, NA),
      similar_health_w2 = ifelse(issue_txt == "Healthcare", PERDIFF_6, NA),
      similar_civil_w2 = ifelse(issue_txt == "Civil rights issues", PERDIFF_6, NA),
      similar_crime_w2 = ifelse(issue_txt == "Law and justice", PERDIFF_6, NA),
      similar_foreign_w2 = ifelse(issue_txt == "Foreign affairs", PERDIFF_6, NA),
      similar_supreme_w2 = ifelse(issue_txt == "The Supreme Court", PERDIFF_6, NA),
      similar_ind_party_welfare_w2 = ifelse(issue_txt == "Social services", PERDIFF_IND_6, NA),
      similar_ind_party_health_w2 = ifelse(issue_txt == "Healthcare", PERDIFF_IND_6, NA),
      similar_ind_party_civil_w2 = ifelse(issue_txt == "Civil rights issues", PERDIFF_IND_6, NA),
      similar_ind_party_crime_w2 = ifelse(issue_txt == "Law and justice", PERDIFF_IND_6, NA),
      similar_ind_party_foreign_w2 = ifelse(issue_txt == "Foreign affairs", PERDIFF_IND_6, NA),
      similar_ind_party_supreme_w2 = ifelse(issue_txt == "The Supreme Court", PERDIFF_IND_6, NA)
    ) %>%
    
    # Extract participation variables
    mutate(
      part_petition_w2 = ifelse(grepl("^1,|^1$", .$Q347), 1, 0),
      part_donate_w2 = ifelse(grepl("2,|,2$|^2$", .$Q347), 1, 0),
      part_share_w2 = ifelse(grepl(",3|^3", .$Q347), 1, 0),
      part_hearing_w2 = ifelse(grepl(",4|^4", .$Q347), 1, 0),
      part_demo_w2 = ifelse(grepl(",5|^5", .$Q347), 1, 0),
      part_contact_w2 = ifelse(grepl(",6|^6", .$Q347), 1, 0),
      part_volunt_w2 = ifelse(grepl(",7|^7", .$Q347), 1, 0),
      part_media_w2 = ifelse(grepl(",8|^8", .$Q347), 1, 0),
      part_vote_w2 = ifelse(grepl(",9|^9", .$Q347), 1, 0)
    ) %>%
    # Rename the rest
    rename(
      "start_date_us_w2" = StartDate_w2,
      "end_date_us_w2" = EndDate,
      "meta_status_w2" = Status,
      "meta_progress_w2" = Progress,
      "meta_duration_w2" = Duration..in.seconds.,
      "meta_finished_w2" = Finished,
      "meta_recdate_w2" = RecordedDate,
      "meta_browser_w2" = META_Browser,
      "meta_version_w2" = META_Version,
      "meta_os_w2" = META_Operating.System,
      "meta_res_w2" = META_Resolution,
      "int_travel_w2" = INTERESTS_TRAVEL,
      "int_politics_w2" = INTERESTS_POL,
      "int_sports_w2" = INTERESTS_SPORTS,
      "int_tv_w2" = INTERESTS_TV,
      "int_vgames_w2" = INTERESTS_VG,
      "int_health_w2" = INTERESTS_HF,
      "int_opentopic_w2" = OPEN_TOPIC,
      "nfcog_1_w2" = COG_1,
      "nfcog_2_w2" = COG_2,
      "nfcog_3_w2" = COG_3,
      "nfcog_4_w2" = COG_4,
      "nfcog_5_w2" = COG_5,
      "nfcog_6_w2" = COG_6,
      "defconf_1_w2" = DEF_1,
      "defconf_2_w2" = DEF_2,
      "defconf_3_w2" = DEF_3,
      "defconf_4_w2" = DEF_4,
      "defconf_5_w2" = DEF_5,
      "polknow_1_w2" = KNOW_1,
      "polknow_2_w2" = KNOW_2,
      "polknow_3_w2" = KNOW_3,
      "polknow_4_w2" = KNOW_4,
      "polknow_5_w2" = KNOW_5,
      "party_w2" = PARTY,
      "party_change_w2" = CHANGE_PARTY,
      "party_strength_1_w2" = PIDID_1,
      "party_strength_2_w2" = PIDID_2,
      "party_strength_3_w2" = PIDID_3,
      "party_strength_4_w2" = PIDID_4,
      "ideo_w2" = IDEO_1,
      "ideo_change_w2" = CHANGE_IDEO,
      "days_week_tv_w2" = tv,
      "days_week_print_w2" = news,
      "days_week_radio_w2" = radio,
      "days_week_web_w2" = online,
      "days_week_sm_w2" = social,
      "days_week_msg_w2" = msg,
      "days_week_f2f_w2" = f2f,
      "days_week_app_w2" = alerts,
      "days_week_search_w2" = search,
      "days_week_change_w2" = OUTLET_TEXT,
      "newsdiet_ideo_w2" = LEAN,
      "know_breitbart_w2" = breitbart,
      "know_blaze_w2" = blaze,
      "know_spect_w2" = am_spectator,
      "know_demnow_w2" = dem_now,
      "know_motherj_w2" = mj,
      "know_nation_w2" = nation,
      "att_econ_1_w2" = ECON_1,
      "att_econ_2_w2" = ECON_2,
      "att_econ_3_w2" = ECON_3,
      "att_welfare_1_w2" = SOCSEC_1,
      "att_welfare_2_w2" = SOCSEC_2,
      "att_welfare_3_w2" = SOCSEC_3,
      "att_health_1_w2" = HEALTH_1,
      "att_health_2_w2" = HEALTH_2,
      "att_health_3_w2" = HEALTH_3,
      "att_civil_1_w2" = RACEREL_1,
      "att_civil_2_w2" = RACEREL_2,
      "att_civil_3_w2" = RACEREL_3,
      "att_civil_4_w2" = RACEREL_4,
      "att_civil_5_w2" = RACEREL_5,
      "att_civil_6_w2" = RACEREL_6,
      "att_crime_1_w2" = CRIME_1,
      "att_crime_2_w2" = CRIME_2,
      "att_crime_3_w2" = CRIME_3,
      "att_foreign_1_w2" = INTER_1,
      "att_foreign_2_w2" = INTER_2,
      "att_foreign_3_w2" = INTER_3,
      "att_climate_1_w2" = ENV_1,
      "att_climate_2_w2" = ENV_2,
      "att_climate_3_w2" = ENV_3,
      "att_immigrant_1_w2" = IMMIG_1,
      "att_immigrant_2_w2" = IMMIG_2,
      "att_immigrant_3_w2" = IMMIG_3,
      "att_gun_1_w2" = GUN_1,
      "att_gun_2_w2" = GUN_2,
      "att_gun_3_w2" = GUN_3,
      "att_supreme_1_w2" = SUPREME_1,
      "att_supreme__2_w2" = SUPREME_2,
      "att_supreme__3_w2" = SUPREME_3,
      "att_trump_1_w2" = TRUMP_1,
      "att_trump_2_w2" = TRUMP_2,
      "att_trump_3_w2" = TRUMP_3,
      "strong_gun_w2" = ISSTRONG_1,
      "strong_immigrant_w2" = ISSTRONG_2,
      "strong_climate_w2" = ISSTRONG_3,
      "strong_trump_w2" = ISSTRONG_4,
      "strong_econ_w2" = ISSTRONG_5,
      "important_gun_w2" = ISPERSO_1,
      "important_immigrant_w2" = ISPERSO_2,
      "important_climate_w2" = ISPERSO_3,
      "important_trump_w2" = ISPERSO_4,
      "important_econ_w2" = ISPERSO_5,
      "disagr_talk_w2" = DIS_1,
      "disagr_msg_w2" = DIS_2,
      "disagr_offline_w2" = DIS_3,
      "disagr_sm_w2" = DIS_4,
      "disagr_internet_w2" = DIS_5,
      "understand_gun_w2" = GRUNDERSTAND_1,
      "understand_immigrant_w2" = GRUNDERSTAND_2,
      "understand_climate_w2" = GRUNDERSTAND_3,
      "understand_trump_w2" = GRUNDERSTAND_4,
      "understand_econ_w2" = GRUNDERSTAND_5,
      "understand_party_opp_w2" = GRUNDERSTAND_7,
      "understand_party_w2" = GRUNDERSTAND_8,
      "understand_ideo_opp_w2" = GRUNDERSTAND_9,
      "understand_ideo_w2" = GRUNDERSTAND_10,
      "ft_dem_supporters_w2" = THERMO_4,
      "ft_rep_supporters_w2" = THERMO_5,
      "ft_liberals_w2" = THERMO_6,
      "ft_conservatives_w2" = THERMO_7,
      "ft_gun_w2" = THERMO_9,
      "ft_immigrant_w2" = THERMO_10,
      "ft_climate_w2" = THERMO_11,
      "ft_econ_w2" = THERMO_12,
      "ft_trump_pres_w2" = THERMO_16,
      "ft_trump_w2" = THERMO_19,
      "mean_party_opp_w2" = ATTOTHER2_1,
      "selfish_party_opp_w2" = ATTOTHER2_2,
      "stupid_party_opp_w2" = ATTOTHER2_3,
      "socdis_party_opp_w2" = SOCDIS_PARTY,
      "talk_party_opp_w2" = ATTOTHER3_1,
      "findout_party_opp_w2" = ATTOTHER3_2,
      "spendtime_party_opp_w2" = ATTOTHER3_3,
      "similar_gun_w2" = PERDIFF_1,
      "similar_immigrant_w2" = PERDIFF_2,
      "similar_climate_w2" = PERDIFF_3,
      "similar_trump_w2" = PERDIFF_4,
      "similar_econ_w2" = PERDIFF_5,
      "mean_ind_party_w2" = ATTOTHER_2_IND_1,
      "selfish_ind_party_w2" = ATTOTHER_2_IND_2,
      "stupid_ind_party_w2" = ATTOTHER_2_IND_3,
      "talk_ind_party_w2" = ATTOTHER_3_IND_1,
      "findout_ind_party_w2" = ATTOTHER_3_IND_2,
      "spend_ind_party_w2" = ATTOTHER_3_IND_3,
      "similar_gun_ind_w2" = PERDIFF_IND_1,
      "similar_immigrant_ind_right_w2" = PERDIFF_IND_2,
      "similar_climate_ind_right_w2" = PERDIFF_IND_3,
      "similar_trump_ind_right_w2" = PERDIFF_IND_4,
      "similar_econ_ind_right_w2" = PERDIFF_IND_5,
      "mean_ideo_opp_w2" = ATTIDEO1_1,
      "selfish_ideo_opp_w2" = ATTIDEO1_2,
      "stupid_ideo_opp_w2" = ATTIDEO1_3,
      "talk_ideo_opp_w2" = ATTIDEO02_1,
      "findout_ideo_opp_w2" = ATTIDEO02_2,
      "spendtime_ideo_opp_w2" = ATTIDEO02_3,
      "socdis_ideo_opp_w2" = SOCDIS_IDEO,
      "mean_ind_ideo_opp_w2" = ATTIDEO1_IND_1,
      "selfish_ind_ideo_opp_w2" = ATTIDEO1_IND_2,
      "stupid_ind_ideo_opp_w2" = ATTIDEO1_IND_3,
      "talk_ind_ideo_opp_w2" = ATTIDEO2_IND_1,
      "findout_ind_ideo_opp_w2" = ATTIDEO2_IND_2,
      "spendtime_ind_ideo_opp_w2" = ATTIDEO2_IND_3,
      "trust_gov_w2" = TRUST_4,
      "trust_police_w2" = TRUST_5,
      "trust_science_w2" = TRUST_6,
      "trust_media_w2" = TRUST_7,
      "trust_scourt_w2" = TRUST_8,
      "trust_profs_w2" = TRUST_13,
      "supp_compr_1_w2" = COMPROMISE_1,
      "supp_compr_2_w2" = COMPROMISE_2,
      "supp_compr_3_w2" = COMPROMISE_3,
      "supp_compr_4_w2" = COMPROMISE_4,
      "malvol_1_w2" = MALVOL_1,
      "malvol_2_w2" = MALVOL_2,
      "malvol_3_w2" = MALVOL_3,
      "malvol_4_w2" = MALVOL_4,
      "malvol_5_w2" = MALVOL_5,
      "malvol_ind_1_w2" = MALVOL.OWN_1,
      "malvol_ind_2_w2" = MALVOL.OWN_2,
      "malvol_ind_3_w2" = MALVOL.OWN_3,
      "malvol_ind_4_w2" = MALVOL.OWN_4,
      "malvol_ind_5_w2" = MALVOL.OWN_5,
      "perpol_1_w2" = PERPOL_1,
      "perpol_2_w2" = PERPOL_2,
      "perpol_3_w2" = PERPOL_3,
      "perpol_4_w2" = PERPOL_4,
      "conflict_int_1_w2" = INTEREST_ITEMS_1,
      "conflict_int_2_w2" = INTEREST_ITEMS_2,
      "conflict_int_3_w2" = INTEREST_ITEMS_3,
      "change_int_w2" = INTEREST_3YEARS,
      "change_int_text_w2" = INTEREST_3YEARS_TEXT,
      "conflict_part_1_w2" = Q349_1,
      "conflict_part_2_w2" = Q349_2,
      "conflict_part_3_w2" = Q349_3,
      "change_part_w2" = Q350,
      "change_part_text_w2" = Q351,
      "know_biden_w2" = DEM_PRES_FAM_1,
      "know_buttigieg_w2" = DEM_PRES_FAM_2,
      "know_harris_w2" = DEM_PRES_FAM_3,
      "know_sanders_w2" = DEM_PRES_FAM_4,
      "know_warren_w2" = DEM_PRES_FAM_5,
      "ft_biden_w2" = DEM_PRES_9,
      "ft_buttigieg_w2" = DEM_PRES_7,
      "ft_harris_w2" = DEM_PRES_5,
      "ft_sanders_w2" = DEM_PRES_4,
      "ft_warren_w2" = DEM_PRES_6,
      "att_tuition_w2" = PROG_POL_1,
      "att_ice_w2" = PROG_POL_2,
      "att_loans_w2" = PROG_POL_3,
      "att_privhealth_w2" = PROG_POL_4,
      "place_immig_w2" = IDEOK_ECON,
      "place_welfare_w2" = IDEOK_IMM,
      "misinfo_1_w2" = FALSE_1,
      "misinfo_2_w2" = FALSE_2,
      "misinfo_3_w2" = FALSE_3,
      "misinfo_4_w2" = FALSE_4,
      "misinfo_5_w2" = FALSE_5,
      "twitter_account_w2" = TWITTER,
      "twitter_consent_w2" = TW_CONSENT,
      "percent_this_comp_w2" = DEVICE_1,
      "percent_other_comp_w2" = DEVICE_4,
      "percent_phone_w2" = DEVICE_2,
      "percent_tablet_w2" = DEVICE_3,
      "people_browser_w2" = PPL_USE,
      "percent_browser_own_w2" = PERCENT_OWN,
      "fb_ctl_w2" = FB_EXP_CTRL,
      "fb_treat_w2" = FB_EXP_TREAT,
      "fb_ctl_consent_w2" = FB_CONTROL_CONSENT,
      "fb_treat_consent_w2" = FB_TREAT_CONSENT,
      "embed_tv_days_w2" = tv_days,
      "embed_tv_media_w2" = tv_media,
      "embed_print_days_w2" = print_days,
      "embed_print_media_w2" = print_media,
      "embed_radio_days_w2" = radio_days,
      "embed_radio_media_w2" = radio_media,
      "embed_web_days_w2" = web_days,
      "embed_web_media_w2" = web_media,
      "embed_social_days_w2" = social_days,
      "embed_social_media_w2" = social_media,
      "embed_msg_days_w2" = msg_days,
      "embed_messaging_media_w2" = messaging_media,
      "embed_f2f_days_w2" = f2f_days,
      "embed_app_days_w2" = app_days,
      "embed_mobile_media_w2" = mobile_media,
      "embed_search_days_w2" = search_days,
      "embed_ideo_w2" = ideo,
      "embed_ideo_imputed_w2" = ideo_imputed,
      "embed_op_ideo_w2" = op_ideo,
      "embed_party_w2" = party,
      "embed_imputed_party_w2" = imputed_party,
      "embed_imputed_party_name_w2" = imputed_party_name,
      "embed_op_party_w2" = op_party,
      "embed_op_party_name_w2" = op_party_name,
      "embed_op_ideo_of_party_w2" = op_ideo_of_party,
      "embed_mixed_party_ideo_w2" = mixed_party_ideo,
      "embed_condition_fb_w2" = condition_fb,
      "embed_rand_first_w2" = rand_first,
      "embed_rand_second_w2" = rand_second,
      "embed_issue_txt_w2" = issue_txt,
      "embed_op_partyW2_w2" = op_partyW2,
      "embed_partyW2_w2" = partyW2,
      "embed_party_nameW2_w2" = party_nameW2,
      "embed_op_party_nameW2_w2" = op_party_nameW2,
      "embed_in_party_w2" = in.party,
      "embed_imputed_partyW2_w2" = imputed_partyW2,
      "embed_imputed_party_nameW2_w2" = imputed_party_nameW2,
      "embed_party_switch_w2" = party_switch,
      "embed_switch_partyW2_w2" = switch_partyW2,
      "embed_link1_w2" = link1,
      "embed_link2_w2" = link2,
      "embed_link3_w2" = link3,
      "embed_ideoW2_w2" = ideoW2,
      "embed_op_ideoW2_w2" = op_ideoW2,
      "embed_ideo_nameW2_w2" = ideo_nameW2,
      "embed_switch_ideoW2_w2" = switch_ideoW2,
      "embed_finish_date_w2" = finish_date
    ) %>%
    # Misinformation
    mutate(across(starts_with("misinfo_"), ~case_when(
      . == 3 ~ 4,
      . == 4 ~ 5,
      . == 5 ~ 3,
      TRUE ~ as.numeric(.)))) %>%
    # Dates 
    mutate(start_date_us_w2 = as.POSIXct(
      start_date_us_w2, format = "%Y-%m-%d %H:%M:%S", tz = "MST"),
      end_date_us_w2 = as.POSIXct(
        end_date_us_w2, format = "%Y-%m-%d %H:%M:%S", tz = "MST")) %>%
  
    # Drop unneeded variables
    select(-c(
      Q347, ISSTRONG_6, ISPERSO_6, THERMO_13,
      ATTIS_EVAL_1_1, ATTIS_EVAL_1_2, ATTIS_EVAL_1_3,
      ATTIS_EVAL_2_1, ATTIS_EVAL_2_2, ATTIS_EVAL_2_3,
      ATTIS_EVAL_3_1, ATTIS_EVAL_3_2, ATTIS_EVAL_3_3,
      PERDIFF_6, PERDIFF_IND_6, GRUNDERSTAND_6
    ))
  
  return(data)
  
}


#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# W3 recoding ####
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

recode_US_w3 <- function(data) {
  
  data <- data %>%
    mutate(

      # Take care of some Qualtrics coding errors
      ISPERSO_1 = case_when(
        ISPERSO_1 == 8 ~ 6,
        ISPERSO_1 == 9 ~ 7,
        TRUE ~ as.numeric(ISPERSO_1)
      ),
      ISPERSO_2 = case_when(
        ISPERSO_2 == 8 ~ 6,
        ISPERSO_2 == 9 ~ 7,
        TRUE ~ as.numeric(ISPERSO_2)
      ),
      ISPERSO_3 = case_when(
        ISPERSO_3 == 8 ~ 6,
        ISPERSO_3 == 9 ~ 7,
        TRUE ~ as.numeric(ISPERSO_3)
      ),
      ISPERSO_4 = case_when(
        ISPERSO_4 == 8 ~ 6,
        ISPERSO_4 == 9 ~ 7,
        TRUE ~ as.numeric(ISPERSO_4)
      ),
      ISPERSO_5 = case_when(
        ISPERSO_5 == 8 ~ 6,
        ISPERSO_5 == 9 ~ 7,
        TRUE ~ as.numeric(ISPERSO_5)
      ),
      ISPERSO_6 = case_when(
        ISPERSO_6 == 8 ~ 6,
        ISPERSO_6 == 9 ~ 7,
        TRUE ~ as.numeric(ISPERSO_6)
      ),
      ECON_1 = case_when(
        ECON_1 == 101 ~ 8,
        ECON_1 == 102 ~ 9,
        ECON_1 == 103 ~ 10,
        ECON_1 == 104 ~ 11,
        ECON_1 == 105 ~ 12,
        ECON_1 == 106 ~ 13,
        TRUE ~ as.numeric(ECON_1)
      ),
      ECON_2 = case_when(
        ECON_2 == 101 ~ 8,
        ECON_2 == 102 ~ 9,
        ECON_2 == 103 ~ 10,
        ECON_2 == 104 ~ 11,
        ECON_2 == 105 ~ 12,
        ECON_2 == 106 ~ 13,
        TRUE ~ as.numeric(ECON_2)
      ),
      ECON_3 = case_when(
        ECON_3 == 101 ~ 8,
        ECON_3 == 102 ~ 9,
        ECON_3 == 103 ~ 10,
        ECON_3 == 104 ~ 11,
        ECON_3 == 105 ~ 12,
        ECON_3 == 106 ~ 13,
        TRUE ~ as.numeric(ECON_3)
      )
    ) %>%
    # Extract personally important issue variables
    mutate(
      ## Issue strength
      strong_welfare_w3 = ifelse(issue_txt == "Social services", ISSTRONG_6, NA),
      strong_health_w3 = ifelse(issue_txt == "Healthcare", ISSTRONG_6, NA),
      strong_civil_w3 = ifelse(issue_txt == "Civil rights issues", ISSTRONG_6, NA),
      strong_crime_w3 = ifelse(issue_txt == "Law and justice", ISSTRONG_6, NA),
      strong_foreign_w3 = ifelse(issue_txt == "Foreign affairs", ISSTRONG_6, NA),
      strong_supreme_w3 = ifelse(issue_txt == "The Supreme Court", ISSTRONG_6, NA),
      ## Issue relevance
      important_welfare_w3 = ifelse(issue_txt == "Social services", ISPERSO_6, NA),
      important_health_w3 = ifelse(issue_txt == "Healthcare", ISPERSO_6, NA),
      important_civil_w3 = ifelse(issue_txt == "Civil rights issues", ISPERSO_6, NA),
      important_crime_w3 = ifelse(issue_txt == "Law and justice", ISPERSO_6, NA),
      important_foreign_w3 = ifelse(issue_txt == "Foreign affairs", ISPERSO_6, NA),
      important_supreme_w3 = ifelse(issue_txt == "The Supreme Court", ISPERSO_6, NA),
      ## Understanding for people with opposite opinion
      understand_welfare_w3 = ifelse(issue_txt == "Social services", GRUNDERSTAND_6, NA),
      understand_health_w3 = ifelse(issue_txt == "Healthcare", GRUNDERSTAND_6, NA),
      understand_civil_w3 = ifelse(issue_txt == "Civil rights issues", GRUNDERSTAND_6, NA),
      understand_crime_w3 = ifelse(issue_txt == "Law and justice", GRUNDERSTAND_6, NA),
      understand_foreign_w3 = ifelse(issue_txt == "Foreign affairs", GRUNDERSTAND_6, NA),
      understand_supreme_w3 = ifelse(issue_txt == "The Supreme Court", GRUNDERSTAND_6, NA),
      ## FT for people with opposite opinion
      ft_welfare_w3 = ifelse(issue_txt == "Social services", THERMO_13, NA),
      ft_health_w3 = ifelse(issue_txt == "Healthcare", THERMO_13, NA),
      ft_civil_w3 = ifelse(issue_txt == "Civil rights issues", THERMO_13, NA),
      ft_crime_w3 = ifelse(issue_txt == "Law and justice", THERMO_13, NA),
      ft_foreign_w3 = ifelse(issue_txt == "Foreign affairs", THERMO_13, NA),
      ft_supreme_w3 = ifelse(issue_txt == "The Supreme Court", THERMO_13, NA)
    ) %>%
    # Selfish/mean/stupid
    mutate(
      mean_gun_w3 = case_when(
        rand_first == "Gun control" ~ ATTIS_EVAL_1_1,
        rand_second == "Gun control" ~ ATTIS_EVAL_2_1
      ),
      mean_immigrant_w3 = case_when(
        rand_first == "Immigration" ~ ATTIS_EVAL_1_1,
        rand_second == "Immigration" ~ ATTIS_EVAL_2_1
      ),
      mean_climate_w3 = case_when(
        rand_first == "Climate change" ~ ATTIS_EVAL_1_1,
        rand_second == "Climate change" ~ ATTIS_EVAL_2_1
      ),
      mean_trump_w3 = case_when(
        rand_first == "Presidency of Donald Trump" ~ ATTIS_EVAL_1_1,
        rand_second == "Presidency of Donald Trump" ~ ATTIS_EVAL_2_1
      ),
      mean_econ_w3 = case_when(
        rand_first == "Economy" ~ ATTIS_EVAL_1_1,
        rand_second == "Economy" ~ ATTIS_EVAL_2_1
      ),
      mean_welfare_w3 = ifelse(issue_txt == "Social services", ATTIS_EVAL_3_1, NA),
      mean_health_w3 = ifelse(issue_txt == "Healthcare", ATTIS_EVAL_3_1, NA),
      mean_civil_w3 = ifelse(issue_txt == "Civil rights issues", ATTIS_EVAL_3_1, NA),
      mean_crime_w3 = ifelse(issue_txt == "Law and justice", ATTIS_EVAL_3_1, NA),
      mean_foreign_w3 = ifelse(issue_txt == "Foreign affairs", ATTIS_EVAL_3_1, NA),
      mean_supreme_w3 = ifelse(issue_txt == "The Supreme Court", ATTIS_EVAL_3_1, NA),
      selfish_gun_w3 = case_when(
        rand_first == "Gun control" ~ ATTIS_EVAL_1_2,
        rand_second == "Gun control" ~ ATTIS_EVAL_2_2
      ),
      selfish_immigrant_w3 = case_when(
        rand_first == "Immigration" ~ ATTIS_EVAL_1_2,
        rand_second == "Immigration" ~ ATTIS_EVAL_2_2
      ),
      selfish_climate_w3 = case_when(
        rand_first == "Climate change" ~ ATTIS_EVAL_1_2,
        rand_second == "Climate change" ~ ATTIS_EVAL_2_2
      ),
      selfish_trump_w3 = case_when(
        rand_first == "Presidency of Donald Trump" ~ ATTIS_EVAL_1_2,
        rand_second == "Presidency of Donald Trump" ~ ATTIS_EVAL_2_2
      ),
      selfish_econ_w3 = case_when(
        rand_first == "Economy" ~ ATTIS_EVAL_1_2,
        rand_second == "Economy" ~ ATTIS_EVAL_2_2
      ),
      selfish_welfare_w3 = ifelse(issue_txt == "Social services", ATTIS_EVAL_3_2, NA),
      selfish_health_w3 = ifelse(issue_txt == "Healthcare", ATTIS_EVAL_3_2, NA),
      selfish_civil_w3 = ifelse(issue_txt == "Civil rights issues", ATTIS_EVAL_3_2, NA),
      selfish_crime_w3 = ifelse(issue_txt == "Law and justice", ATTIS_EVAL_3_2, NA),
      selfish_foreign_w3 = ifelse(issue_txt == "Foreign affairs", ATTIS_EVAL_3_2, NA),
      selfish_supreme_w3 = ifelse(issue_txt == "The Supreme Court", ATTIS_EVAL_3_2, NA),
      stupid_gun_w3 = case_when(
        rand_first == "Gun control" ~ ATTIS_EVAL_1_3,
        rand_second == "Gun control" ~ ATTIS_EVAL_2_3
      ),
      stupid_immigrant_w3 = case_when(
        rand_first == "Immigration" ~ ATTIS_EVAL_1_3,
        rand_second == "Immigration" ~ ATTIS_EVAL_2_3
      ),
      stupid_climate_w3 = case_when(
        rand_first == "Climate change" ~ ATTIS_EVAL_1_3,
        rand_second == "Climate change" ~ ATTIS_EVAL_2_3
      ),
      stupid_trump_w3 = case_when(
        rand_first == "Presidency of Donald Trump" ~ ATTIS_EVAL_1_3,
        rand_second == "Presidency of Donald Trump" ~ ATTIS_EVAL_2_3
      ),
      stupid_econ_w3 = case_when(
        rand_first == "Economy" ~ ATTIS_EVAL_1_3,
        rand_second == "Economy" ~ ATTIS_EVAL_2_3
      ),
      stupid_welfare_w3 = ifelse(issue_txt == "Social services", ATTIS_EVAL_3_3, NA),
      stupid_health_w3 = ifelse(issue_txt == "Healthcare", ATTIS_EVAL_3_3, NA),
      stupid_civil_w3 = ifelse(issue_txt == "Civil rights issues", ATTIS_EVAL_3_3, NA),
      stupid_crime_w3 = ifelse(issue_txt == "Law and justice", ATTIS_EVAL_3_3, NA),
      stupid_foreign_w3 = ifelse(issue_txt == "Foreign affairs", ATTIS_EVAL_3_3, NA),
      stupid_supreme_w3 = ifelse(issue_txt == "The Supreme Court", ATTIS_EVAL_3_3, NA)
    ) %>%
    # Extract participation variables
    mutate(
      part_petition_w3 = ifelse(grepl("^1,|^1$", .$PARTICIPATION_2020), 1, 0),
      part_donate_w3 = ifelse(grepl("2,|,2$|^2$", .$PARTICIPATION_2020), 1, 0),
      part_share_w3 = ifelse(grepl(",3|^3", .$PARTICIPATION_2020), 1, 0),
      part_hearing_w3 = ifelse(grepl(",4|^4", .$PARTICIPATION_2020), 1, 0),
      part_demo_w3 = ifelse(grepl(",5|^5", .$PARTICIPATION_2020), 1, 0),
      part_contact_w3 = ifelse(grepl(",6|^6", .$PARTICIPATION_2020), 1, 0),
      part_volunt_w3 = ifelse(grepl(",7|^7", .$PARTICIPATION_2020), 1, 0),
      part_media_w3 = ifelse(grepl(",8|^8", .$PARTICIPATION_2020), 1, 0),
      part_vote_w3 = ifelse(grepl(",9|^9", .$PARTICIPATION_2020), 1, 0)
    ) %>%
    # political knowledge
    mutate(polknow_pres_w3 = ifelse(
      POLKNOW_PRES %in% c("2", "2 ", "2 or 3 if serving a partial", 
                          "2 consecutive", "2 consecutive terms", "2 terms",
                          "2 terms at 4yrs elected", 
                          "2 terms, or for a total of 10 years",
                          "no more than two consecutive terms", "twice", 
                          "Twice", "Two", "two terms", "Two terms. "), 1, 0),
      polknow_majority_w3 = ifelse(
        POLKNOW_MAJORITY == 2, 1, 0),
      polknow_senator_w3 = ifelse(
        POLKNOW_SENATOR %in% c("6", "6 ", "6 (1/3 replaced every 2 years)", 
                               "6 year term", "6 years", "six", "Six"), 1, 0),
      polknow_spend_w3 = ifelse(POLKNOW_SPEND == "1", 1, 0)) %>%
    
    # Following politics
    mutate(follow_pol_w3 = case_when(
      FOLLOW == 1 ~ 1,
      FOLLOW == 4 ~ 2,
      FOLLOW == 6 ~ 3,
      FOLLOW == 7 ~ 4,
      FOLLOW == 8 ~ 5,
      FOLLOW == 20 ~ 6,
      FOLLOW == 21 ~ 7)) %>%
    
    # Rename the rest
    rename(
      "start_date_us_w3" = StartDate_w3,
      "end_date_us_w3" = EndDate,
      "meta_status_w3" = Status,
      "meta_progress_w3" = Progress,
      "meta_duration_w3" = Duration..in.seconds.,
      "meta_finished_w3" = Finished,
      "meta_recdate_w3" = RecordedDate,
      "meta_browser_w3" = META_Browser,
      "meta_version_w3" = META_Version,
      "meta_os_w3" = META_Operating.System,
      "meta_res_w3" = META_Resolution,
      "retake_w3" = RETAKE,
      "days_week_tv_w3" = DAYSNEWS_1,
      "days_week_print_w3" = DAYSNEWS_2,
      "days_week_radio_w3" = DAYSNEWS_3,
      "days_week_web_w3" = DAYSNEWS_4,
      "days_week_sm_w3" = DAYSNEWS_5,
      "days_week_msg_w3" = DAYSNEWS_6,
      "days_week_f2f_w3" = DAYSNEWS_7,
      "days_week_app_w3" = DAYSNEWS_8,
      "days_week_search_w3" = DAYSNEWS_9,
      "polknow_imp_tv_w3" = NEWS_IMP_1,
      "polknow_imp_print_w3" = NEWS_IMP_2,
      "polknow_imp_radio_w3" = NEWS_IMP_3,
      "polknow_imp_web_w3" = NEWS_IMP_4,
      "polknow_imp_sm_w3" = NEWS_IMP_5,
      "polknow_imp_msg_w3" = NEWS_IMP_6,
      "polknow_imp_f2f_w3" = NEWS_IMP_7,
      "polknow_imp_app_w3" = NEWS_IMP_8,
      "polknow_imp_search_w3" = NEWS_IMP_9,
      "change_tv_1_w3" = NEWSCHANGE_TV_1,
      "change_tv_2_w3" = NEWSCHANGE_TV_2,
      "change_print_1_w3" = NEWSCHANGE_PRINT_1,
      "change_print_2_w3" = NEWSCHANGE_PRINT_2,
      "change_radio_1_w3" = NEWSCHANGE_RADIO_1,
      "change_radio_2_w3" = NEWSCHANGE_RADIO_2,
      "change_web_1_w3" = NEWSCHANGE_WEB_1,
      "change_web_2_w3" = NEWSCHANGE_WEB_2,
      "change_sm_1_w3" = NEWSCHANGE_SOCIAL_1,
      "change_sm_2_w3" = NEWSCHANGE_SOCIAL_2,
      "change_msg_1_w3" = NEWSCHANGE_MSG_1,
      "change_msg_2_w3" = NEWSCHANGE_MSG_2,
      "change_app_1_w3" = NEWSCHANGE_APP_1,
      "change_app_2_w3" = NEWSCHANGE_APP_2,
      "newsdiet_ideo_w3" = LEAN,
      "selexpo_1_w3" = SEL_1,
      "selexpo_2_w3" = SEL_2,
      "selexpo_3_w3" = SEL_3,
      "selexpo_4_w3" = SEL_4,
      "selexpo_5_w3" = SEL_5,
      "selexpo_redo_1_w3" = SE_SL_REDO_1,
      "selexpo_redo_2_w3" = SE_SL_REDO_2,
      "selexpo_redo_3_w3" = SE_SL_REDO_3,
      "selexpo_redo_4_w3" = SE_SL_REDO_4,
      "selexpo_redo_5_w3" = SE_SL_REDO_5,
      "int_politics_w3" = POL_INT,
      "party_w3" = PID,
      "party_change_w3" = CHANGE_PARTY,
      "party_strength_1_w3" = PIDID_1,
      "party_strength_2_w3" = PIDID_2,
      "party_strength_3_w3" = PIDID_3,
      "party_strength_4_w3" = PIDID_4,
      "ideo_w3" = IDEO_1,
      "ideo_change_w3" = CHANGE_IDEO,
      "att_econ_1_w3" = ECON_1,
      "att_econ_2_w3" = ECON_2,
      "att_econ_3_w3" = ECON_3,
      "att_welfare_1_w3" = SOCSEC_1,
      "att_welfare_2_w3" = SOCSEC_2,
      "att_welfare_3_w3" = SOCSEC_3,
      "att_health_1_w3" = HEALTH_1,
      "att_health_2_w3" = HEALTH_2,
      "att_health_3_w3" = HEALTH_3,
      "att_civil_1_w3" = RACEREL_1,
      "att_civil_2_w3" = RACEREL_2,
      "att_civil_3_w3" = RACEREL_3,
      "att_civil_4_w3" = RACEREL_4,
      "att_civil_5_w3" = RACEREL_5,
      "att_civil_6_w3" = RACEREL_6,
      "att_crime_1_w3" = CRIME_1,
      "att_crime_2_w3" = CRIME_2,
      "att_crime_3_w3" = CRIME_3,
      "att_foreign_1_w3" = INTER_1,
      "att_foreign_2_w3" = INTER_2,
      "att_foreign_3_w3" = INTER_3,
      "att_climate_1_w3" = ENV_1,
      "att_climate_2_w3" = ENV_2,
      "att_climate_3_w3" = ENV_3,
      "att_immigrant_1_w3" = IMMIG_1,
      "att_immigrant_2_w3" = IMMIG_2,
      "att_immigrant_3_w3" = IMMIG_3,
      "att_gun_1_w3" = GUN_1,
      "att_gun_2_w3" = GUN_2,
      "att_gun_3_w3" = GUN_3,
      "att_supreme_1_w3" = SUPREME_1,
      "att_supreme_2_w3" = SUPREME_2,
      "att_supreme_3_w3" = SUPREME_3,
      "att_trump_1_w3" = TRUMP_1,
      "att_trump_2_w3" = TRUMP_2,
      "att_trump_3_w3" = TRUMP_3,
      "strong_gun_w3" = ISSTRONG_1,
      "strong_immigrant_w3" = ISSTRONG_2,
      "strong_climate_w3" = ISSTRONG_3,
      "strong_trump_w3" = ISSTRONG_4,
      "strong_econ_w3" = ISSTRONG_5,
      "important_gun_w3" = ISPERSO_1,
      "important_immigrant_w3" = ISPERSO_2,
      "important_climate_w3" = ISPERSO_3,
      "important_trump_w3" = ISPERSO_4,
      "important_econ_w3" = ISPERSO_5,
      "disagr_talk_w3" = DIS_1,
      "disagr_msg_w3" = DIS_2,
      "disagr_offline_w3" = DIS_3,
      "disagr_sm_w3" = DIS_4,
      "disagr_internet_w3" = DIS_5,
      "understand_gun_w3" = GRUNDERSTAND_1,
      "understand_immigrant_w3" = GRUNDERSTAND_2,
      "understand_climate_w3" = GRUNDERSTAND_3,
      "understand_trump_w3" = GRUNDERSTAND_4,
      "understand_econ_w3" = GRUNDERSTAND_5,
      "understand_party_opp_w3" = GRUNDERSTAND_9,
      "understand_party_w3" = GRUNDERSTAND_10,
      "understand_ideo_opp_w3" = GRUNDERSTAND_12,
      "understand_ideo_w3" = GRUNDERSTAND_13,
      "ft_dem_supporters_w3" = THERMO_4,
      "ft_rep_supporters_w3" = THERMO_5,
      "ft_liberals_w3" = THERMO_6,
      "ft_conservatives_w3" = THERMO_7,
      "ft_gun_w3" = THERMO_9,
      "ft_immigrant_w3" = THERMO_10,
      "ft_climate_w3" = THERMO_11,
      "ft_econ_w3" = THERMO_12,
      "ft_trump_pres_w3" = THERMO_16,
      "ft_trump_w3" = THERMO_19,
      "mean_party_opp_w3" = ATTOTHER2_1,
      "selfish_party_opp_w3" = ATTOTHER2_2,
      "stupid_party_opp_w3" = ATTOTHER2_3,
      "socdis_party_opp_w3" = SOCDIS_PARTY,
      "mean_ind_dem_w3" = ATTOTHER_2_IND_1,
      "selfish_ind_dem_w3" = ATTOTHER_2_IND_2,
      "stupid_ind_dem_w3" = ATTOTHER_2_IND_3,
      "socdis_ind_dem_w3" = Q372,
      "mean_ideo_opp_w3" = ATTIDEO1_1,
      "selfish_ideo_opp_w3" = ATTIDEO1_2,
      "stupid_ideo_opp_w3" = ATTIDEO1_3,
      "socdis_ideo_opp_w3" = SOCDIS_IDEO,
      "mean_ind_cons_w3" = ATTIDEO1_IND_1,
      "selfish_ind_cons_w3" = ATTIDEO1_IND_2,
      "stupid_ind_cons_w3" = ATTIDEO1_IND_3,
      "socdis_ind_cons_w3" = Q373,
      "trust_gov_w3" = TRUST_1,
      "trust_police_w3" = TRUST_2,
      "trust_science_w3" = TRUST_3,
      "trust_media_w3" = TRUST_4,
      "trust_scourt_w3" = TRUST_5,
      "trust_profs_w3" = TRUST_6,
      "supp_compr_1_w3" = COMPROMISE_1,
      "supp_compr_2_w3" = COMPROMISE_2,
      "supp_compr_3_w3" = COMPROMISE_3,
      "supp_compr_4_w3" = COMPROMISE_4,
      "malvol_1_w3" = MALVOL_1,
      "malvol_2_w3" = MALVOL_2,
      "malvol_3_w3" = MALVOL_3,
      "malvol_4_w3" = MALVOL_4,
      "malvol_5_w3" = MALVOL_5,
      "malvol_redo_1_w3" = MALVOL_SL_1,
      "malvol_redo_2_w3" = MALVOL_SL_2,
      "malvol_redo_3_w3" = MALVOL_SL_3,
      "malvol_redo_4_w3" = MALVOL_SL_4,
      "malvol_redo_5_w3" = MALVOL_SL_5,
      "malvol_ind_1_w3" = MALVOL.OWN_1,
      "malvol_ind_2_w3" = MALVOL.OWN_2,
      "malvol_ind_3_w3" = MALVOL.OWN_3,
      "malvol_ind_4_w3" = MALVOL.OWN_4,
      "malvol_ind_5_w3" = MALVOL.OWN_5,
      "perpol_1_w3" = PERPOL_1,
      "perpol_2_w3" = PERPOL_2,
      "perpol_3_w3" = PERPOL_3,
      "perpol_4_w3" = PERPOL_4,
      "know_biden_w3" = PRES_FAM_1,
      "know_buttigieg_w3" = PRES_FAM_2,
      "know_harris_w3" = PRES_FAM_3,
      "know_sanders_w3" = PRES_FAM_4,
      "know_warren_w3" = PRES_FAM_5,
      "ft_biden_w3" = PRES_FT_9,
      "ft_buttigieg_w3" = PRES_FT_7,
      "ft_harris_w3" = PRES_FT_5,
      "ft_sanders_w3" = PRES_FT_4,
      "ft_warren_w3" = PRES_FT_6,
      "vote_pres_w3" = PRES_VOTE,
      "vote_pres_text_w3" = PRES_VOTE_7_TEXT,
      "warren_vs_trump_w3" = Q386_1,
      "sanders_vs_trump_w3" = Q386_2,
      "harris_vs_trump_w3" = Q386_3,
      "buttigieg_vs_trump_w3" = Q386_4,
      "biden_vs_trump_w3" = Q386_5,
      "misinfo_1_w3" = FALSE_1,
      "misinfo_2_w3" = FALSE_2,
      "misinfo_3_w3" = FALSE_3,
      "misinfo_4_w3" = FALSE_4,
      "misinfo_5_w3" = FALSE_5,
      "misinfo_6_w3" = FALSE_6,
      "misinfo_7_w3" = FALSE_7,
      "twitter_account_w3" = TWITTER,
      "twitter_consent_w3" = TW_CONSENT,
      "percent_this_comp_w3" = DEVICE_1,
      "percent_other_comp_w3" = DEVICE_4,
      "percent_phone_w3" = DEVICE_2,
      "percent_tablet_w3" = DEVICE_3,
      "people_browser_w3" = PPL_USE,
      "percent_browser_own_w3" = PERCENT_OWN,
      "future_studies_w3" = future,
      "consent_lit_1_w3" = CONSENT_LIT_1,
      "consent_lit_2_w3" = CONSENT_LIT_2,
      "consent_lit_3_w3" = CONSENT_LIT_3,
      "consent_lit_4_w3" = CONSENT_LIT_4,
      "nonews_ctl_w3" = NONEWS_EXP_CTRL,
      "nonews_treat_w3" = NONEWS_EXP_TREAT,
      "nonews_ctl_consent_w3" = NONE_CNTL_CONSENT,
      "nonews_treat_consent_w3" = NONEWS_TREAT_CONSENT,
      "embed_mixed_party_ideo_w3" = mixed_party_ideo,
      "embed_rand_first_w3" = rand_first,
      "embed_rand_second_w3" = rand_second,
      "embed_issue_txt_w3" = issue_txt,
      "embed_partyW2_w3" = partyW2,
      "embed_ideoW2_w3" = ideoW2,
      "embed_w3_group_w3" = w3_group,
      "embed_aihuman_condition_w3" = aihuman_condition,
      "embed_aihuman_condition_procounter_w3" = aihuman_condition_procounter,
      "embed_nonews_condition_w3" = nonews_condition,
      "embed_switch_partyW2_w3" = switch_partyW2,
      "embed_ideoW2_label_w3" = ideoW2_label,
      "embed_w2finish_w3" = w2finish,
      "embed_w3_daycontact_w3" = w3_daycontact,
      "embed_sl_se_w3" = sl_se,
      "embed_sl_malvol_w3" = sl_malvol,
      "embed_finish_dateW3_w3" = finish_dateW3,
      "embed_finish_dateW3_w3" = finish_dateW3,
      "embed_eligible_post_w3" = eligible_post,
      "embed_op_partyW3_w3" = op_partyW3,
      "embed_partyW3_w3" = partyW3,
      "embed_party_nameW3_w3" = party_nameW3,
      "embed_op_party_nameW3_w3" = op_party_nameW3,
      "embed_in_party_w3" = in_party,
      "embed_bin_partyW3_w3" = bin_partyW3,
      "embed_bin_party_nameW3_w3" = bin_party_nameW3,
      "embed_party_switch_w3" = party_switch,
      "embed_in.party_w3" = in.party,
      "embed_ideoW3_w3" = ideoW3,
      "embed_op_ideoW3_w3" = op_ideoW3,
      "embed_ideo_nameW3_w3" = ideo_nameW3,
      "embed_ideoW3_label_w3" = ideoW3_label,
      "embed_switch_ideoW3_w3" = switch_ideoW3,
      "embed_switch_partyW3_w3" = switch_partyW3
    ) %>%
    # Misinformation
    mutate(across(starts_with("misinfo_"), ~case_when(
      . == 3 ~ 4,
      . == 4 ~ 5,
      . == 5 ~ 3,
      TRUE ~ as.numeric(.)))) %>%
    # Dates 
    mutate(start_date_us_w3 = as.POSIXct(
      start_date_us_w3, format = "%Y-%m-%d %H:%M:%S", tz = "MST"),
      end_date_us_w3 = as.POSIXct(
        end_date_us_w3, format = "%Y-%m-%d %H:%M:%S", tz = "MST")) %>%
    # Drop unnecessary variables
    select(-c(
      ISSTRONG_6, ISPERSO_6, THERMO_13,
      ATTIS_EVAL_1_1, ATTIS_EVAL_1_2, ATTIS_EVAL_1_3,
      ATTIS_EVAL_2_1, ATTIS_EVAL_2_2, ATTIS_EVAL_2_3,
      ATTIS_EVAL_3_1, ATTIS_EVAL_3_2, ATTIS_EVAL_3_3,
      GRUNDERSTAND_6, PARTICIPATION_2020,
      POLKNOW_PRES, POLKNOW_MAJORITY, 
      POLKNOW_SENATOR, POLKNOW_SPEND, FOLLOW
    ))
  
  return(data)
  
} 

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Wave joining ####
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

join_US_waves <- function(wave1, wave2 = NULL, wave3 = NULL,
                          wave0a = NULL, wave0b = NULL, 
                          w1_missing_sociodem = NULL,
                          impute_party = FALSE) {
  
  data <- wave1
  
  if (!is.null(wave0b) & !is.null(wave0a)) {
    wave0 <- rbind(wave0a, wave0b) 
    data <- data %>%
      left_join(., wave0, by = "person_id")
  }
  
  if (!is.null(w1_missing_sociodem)) {
    data <- data %>%
      left_join(., w1_missing_sociodem %>%
                  rename("gender_estimated" = gender_w0,
                         "age_estimated" = age_w0,
                         "ethn_estimated" = ethn_w0,
                         "hisp_estimated" = hisp_w0,
                         "edu_estimated" = edu_w0) %>%
                  mutate(gender_estimated = gender_estimated - 1), 
                by = "person_id") %>%
      mutate(age_w0 = ifelse(
        is.na(age_w0) & !is.na(age_estimated), 
        age_estimated, age_w0),
        gender_w0 = ifelse(
          is.na(gender_w0) & !is.na(gender_estimated), 
          gender_estimated, gender_w0),
        ethn_w0 = ifelse(
          is.na(ethn_w0) & !is.na(ethn_estimated), 
          ethn_estimated, ethn_w0),
        hisp_w0 = ifelse(
          is.na(hisp_w0) & !is.na(hisp_estimated), 
          hisp_estimated, hisp_w0),
        edu_w0 = ifelse(
          is.na(edu_w0) & !is.na(edu_estimated), 
          edu_estimated, edu_w0)) %>%
      mutate(
        gender_w0_fac = as.factor(case_when(
          gender_w0 == 0 ~ "Male",
          gender_w0 == 1 ~ "Female")),
        ethn_w0_fac = as.factor(case_when(
          ethn_w0 == 1 ~ "White",
          ethn_w0 == 2 ~ "Black",
          ethn_w0 == 3 ~ "Asian",
          ethn_w0 == 4 ~ "Native American",
          ethn_w0 == 7 ~ "Prefer to self-describe")),
        hisp_w0_fac = as.factor(case_when(
          hisp_w0 == 1 ~ "yes",
          hisp_w0 == 2 ~ "no")),
        edu_w0_fac = as.factor(case_when(
          edu_w0 == 1 ~ "3rd Grade or less",
          edu_w0 == 5 ~ "Middle School - Grades 4-8",
          edu_w0 == 6 ~ "Completed some high school",
          edu_w0 == 8 ~ "High school graduate",
          edu_w0 == 9 ~ "Other post-high school vocational training",
          edu_w0 == 10 ~ "Completed some college, but no degree",
          edu_w0 == 11 ~ "Associate Degree",
          edu_w0 == 12 ~ "College Degree (such as B.A., B.S.)",
          edu_w0 == 13 ~ "Completed some graduate, but no degree",
          edu_w0 == 14 ~ "Master's degree",
          edu_w0 == 15 ~ "Doctorate degree")))
      
  }
  
  if (impute_party == TRUE) {
    data <- data %>%
      mutate(party_w0 = ifelse(
        is.na(party_w0), party_w1, party_w0),
        ideo_w0 = ifelse(
          is.na(ideo_w0), ideo_w1, ideo_w0))
  }

  if (!is.null(wave2)) {
    data <- data %>%
      left_join(., wave2 %>%
                  select(-ResponseId_w1), 
                by = "person_id")
  }
  
  if (!is.null(wave3)) {
    data <- data %>%
      left_join(., wave3 %>%
                  select(-ResponseId_w1), 
                by = "person_id")
  }
  
  return(data)
}

recode_join_US <- function(wave1, wave2 = NULL, wave3 = NULL,
                          wave0a = NULL, wave0b = NULL, 
                          w1_missing_sociodem = NULL,
                          impute_party = FALSE) {
  
  data <- recode_US_w1(wave1)
  
  if (!is.null(wave0b) & !is.null(wave0a)) {
    wave0a <- recode_US_w0(wave0a)
    wave0b <- recode_US_w0(wave0b)
    wave0 <- rbind(wave0a, wave0b) 
    data <- data %>%
      left_join(., wave0, by = "person_id")
  }
  
  if (!is.null(w1_missing_sociodem)) {
    data <- data %>%
      left_join(., w1_missing_sociodem %>%
                  rename(gender_estimated = gender_w0,
                         age_estimated = age_w0,
                         ethn_estimated = ethn_w0,
                         hisp_estimated = hisp_w0,
                         edu_estimated = edu_w0), 
                by = "person_id") %>%
      mutate(age_w0 = ifelse(
        is.na(age_w0) & !is.na(age_estimated), 
        age_estimated, age_w0),
        gender_w0 = ifelse(
          is.na(gender_w0) & !is.na(gender_estimated), 
          gender_estimated, gender_w0),
        ethn_w0 = ifelse(
          is.na(ethn_w0) & !is.na(ethn_estimated), 
          ethn_estimated, ethn_w0),
        hisp_w0 = ifelse(
          is.na(hisp_w0) & !is.na(hisp_estimated), 
          hisp_estimated, hisp_w0),
        edu_w0 = ifelse(
          is.na(edu_w0) & !is.na(edu_estimated), 
          edu_estimated, edu_w0)) %>%
      mutate(
        gender_w0_fac = as.factor(case_when(
          gender_w0 == 0 ~ "Male",
          gender_w0 == 1 ~ "Female")),
        ethn_w0_fac = as.factor(case_when(
          ethn_w0 == 1 ~ "White",
          ethn_w0 == 2 ~ "Black",
          ethn_w0 == 3 ~ "Asian",
          ethn_w0 == 4 ~ "Native American",
          ethn_w0 == 7 ~ "Prefer to self-describe")),
        hisp_w0_fac = as.factor(case_when(
          hisp_w0 == 1 ~ "yes",
          hisp_w0 == 2 ~ "no")),
        edu_w0_fac = as.factor(case_when(
          edu_w0 == 1 ~ "3rd Grade or less",
          edu_w0 == 5 ~ "Middle School - Grades 4-8",
          edu_w0 == 6 ~ "Completed some high school",
          edu_w0 == 8 ~ "High school graduate",
          edu_w0 == 9 ~ "Other post-high school vocational training",
          edu_w0 == 10 ~ "Completed some college, but no degree",
          edu_w0 == 11 ~ "Associate Degree",
          edu_w0 == 12 ~ "College Degree (such as B.A., B.S.)",
          edu_w0 == 13 ~ "Completed some graduate, but no degree",
          edu_w0 == 14 ~ "Master's degree",
          edu_w0 == 15 ~ "Doctorate degree")))
    
  }
  
  if (impute_party == TRUE) {
    data <- data %>%
      mutate(party_w0 = ifelse(
        is.na(party_w0), party_w1, party_w0),
        ideo_w0 = ifelse(
          is.na(ideo_w0), ideo_w1, ideo_w0))
  }
  
  if (!is.null(wave2)) {
    wave2 <- recode_US_w2(wave2)
    data <- data %>%
      left_join(., wave2 %>%
                  select(-ResponseId_w1), 
                by = "person_id")
  }
  
  if (!is.null(wave3)) {
    wave3 <- recode_US_w3(wave3)
    data <- data %>%
      left_join(., wave3 %>%
                  select(-ResponseId_w1), 
                by = "person_id")
  }
  
  return(data)
}


